Interrupt vs Exception
In any computer, during its normal execution of a program, there could be events that can cause the CPU to temporarily halt. Events like this are called interrupts. Interrupts can be caused by either software or hardware faults. Hardware interrupts are called (simply) Interrupts, while software interrupts are called Exceptions. Once an interrupt (software or hardware) is raised, the control is transferred to a special subroutine called ISR (Interrupt Service Routine) that can handle the conditions that are raised by the interrupt.
What is Interrupt?
The term Interrupt is usually reserved for hardware interrupts. They are program control interruptions caused by external hardware events. Here, external means external to the CPU. Hardware interrupts usually come from many different sources such as timer chip, peripheral devices (keyboards, mouse, etc.), I/O ports (serial, parallel, etc.), disk drives, CMOS clock, expansion cards (sound card, video card, etc). That means hardware interrupts almost never occur due to some event related to the executing program. For example, an event like a key press on the keyboard by the user, or an internal hardware timer timing out can raise this kind of interrupt and can inform the CPU that a certain device needs some attention. In a situation like that the CPU will stop what ever it was doing (i.e. pauses the current program), provides the service required by the device and will get back to the normal program. When hardware interrupts occur and the CPU starts the ISR, other hardware interrupts are disabled (e.g. in 80×86 machines). If you need other hardware interrupts to occur while the ISR is running, you need to do that explicitly by clearing the interrupt flag (with sti instruction). In 80×86 machines, clearing the interrupt flag will only affect hardware interrupts.
What is Exceptions?
Exception is a software interrupt, which can be identified as a special handler routine. Exception can be identified as an automatically occurring trap (a Trap can be identified as a transfer of control, which is initiated by the programmer). Generally, there are no specific instructions associated with exceptions (traps are generated using a specific instruction). So, an exception occurs due to an “exceptional” condition that occurs during program execution. For example, division by zero, execution of an illegal opcode or memory related fault could cause exceptions. Whenever an exception is raised, the CPU temporarily suspends the program it was executing and starts the ISR. ISR will contain what to do with the exception. It may correct the problem or if it is not possible it may abort the program gracefully by printing a suitable error message. Although a specific instruction does not cause an exception, an exception will always be caused by an instruction. For example, the division by zero error can only occur during the execution of the division instruction.
What’s the difference between Interrupt and Exception?
Interrupts are hardware interrupts, while exceptions are software interrupts. Occurrences of hardware interrupts usually disable other hardware interrupts, but this is not true for exceptions. If you need to disallow hardware interrupts until an exception is served, you need to explicitly clear the interrupt flag. And usually the interrupt flag on the computer affects (hardware) interrupts as opposed to exceptions. This means that clearing this flag will not prevent exceptions.