Difference between Interrupt and Exception

Prerequisite – Interrupts and Exceptions
Interrupt is one of the classes of Exception. There are 4 classes of Exception- interrupt, trap, fault and abort. Though, interrupt belongs to exception still there are many differences between them.

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 Interrupts, while software interrupts are called Exceptions. Once an interrupt is raised, the control is transferred to a special sub-routine called Interrupt Service Routine (ISR), that can handle the conditions that are raised by the interrupt.

What is Trap, Fault and Abort ?

  1. Trap –
    It is typically a type of synchronous interrupt caused by an exceptional condition (e.g., breakpoint, division by zero, invalid memory access).
  2. Fault –
    Fault exception is used in a client application to catch contractually-specified SOAP faults. By the simple exception message, you can’t identify the reason of the exception, that’s why a Fault Exception is useful.
  3. Abort –
    It is a type of exception occurs when an instruction fetch causes an error.

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.

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 whatever 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 Exception ?
Exception is a software interrupt, which can be identified as a special handler routine. Exception can be identified as an automatically occurring trap. 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.

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.



Difference between Interrupt and Exception :

Interrupt Exception
These are Hardware interrupts. These are Software Interrupts.
Occurrences of hardware interrupts usually disable other hardware interrupts. This is not a true case in terms of Exception.
These are asynchronous external requests for service (like keyboard or printer needs service). These are synchronous internal requests for service based upon abnormal events (think of illegal instructions, illegal address, overflow etc).
Being asynchronous, interrupts can occur at any place in the program. Being synchronous, exceptions occur when there is abnormal event in your program like, divide by zero or illegal memory location.
These are normal events and shouldn’t interfere with the normal running of a computer. These are abnormal events and often result in the termination of a program

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.