# Exceptions Handling
## Interrupts
Interrupts are signals that alter the normal control flow of a program at runtime. An **interrupt** must be handled by a dedicated **interrupt handler**, and the architecture should be able to resume the normal execution of the program.
Causes:
- **Asynchronous** external events:
- I/O device reply
- timer expiration
- HW failure
- **Synchronous** internal event:
- opcode error
- arithmetic overflow
- bad memory access
- traps: traps are a mechanism used by computers to transfer control to the OS or to kernel routines in response to various events like system calls.
**Interrupts** can be characterized in various ways:
- **Async** interrupts are caused by external sources and can be handled at the end of the execution of the current instruction, making them easier to manage than **sync** ones.
- **User request** interrupts are predictable: they are treated as exceptions because they use the same mechanism. **Coerced interrupts** (forced) are caused by some HW event not under control of the program.
- **User maskable** vs **user nonmaskable**
- **Within instructions** interrupts are usually synchronous since the instruction triggers the exception. The instruction must be stopped and restarted.
- **Resume** vs **terminate** interrupts
## Super exception table
| Exception type | Synchronous vs. asynchronous | User request vs. coerced | Oser maskable vs. nonmaskable | Within vs. between instructions | Resume vs. terminate |
|:-------------------------------------------------------:|:-----------------------------:|:-------------------------:|:-------------------------------:|:---------------------------------:|:---------------------:|
| I/O device request | Asynchronous | Coerced | Nonmaskable | Between | Resume |
| Invoke operating system Tracing instruction execution | Synchronous | User request | Nonmaskable | Between | Resume |
| Breakpoint Integer arithmetic overflow | Synchronous | User request | User maskable | Between | Resume |
| Floating-point arithmetic overflow or underflow | Synchronous | Coerced | User maskable | Within | Resume |
| Page fault | Synchronous | Coerced | Nonmaskable | Within | Resume |
| Mischaligned memory accesses | Synchronous | Coerced | User maskable | Within | Resume |
| Memory protection violations | Synchronous | Coerced | Nonmaskable | Within | Resume |
| Using undefined instructions | Synchronous | Coerced | Nonmaskable | Within | Terminate |
| Hardware malfunctions | Asynchronous | Coerced | Nonmaskable | Within | Terminate |
| Power failure | Asynchronous | Coerced | Nonmaskable | Within | Terminate |