Exception handling is the process of responding to the occurrence of exceptions – anomalous or exceptional conditions requiring special processing – during the execution of a program. In general, an exception breaks the normal flow of execution and executes a pre-registered exception handler.
One of the challenges of file processing is that the actual input and output is beyond program control. Behind the scenes, the program is paused while the operating system uses interrupts to request that the storage device read from or write to the file. If a read is successful, data is returned to the program. If the read is unsuccessful, an exception occurs. Possible exception reasons include File Not Found, OS Error, IO Error, Permission Error, Timeout Error, Memory Error, Buffer Error, Encoding Error, etc.
Most current programming languages provide some type of support for exceptions and exception handling.
Exception handling should always be used with any type of processing that is beyond program control.
- Anomalous or exceptional conditions requiring special processing.
- A request for the processor to interrupt currently executing code (when permitted), so that the event can be processed in a timely manner.