Exception Handling
Overview
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.[1]
Discussion
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.
| Language | Key words | Examples |
|---|---|---|
| C++ C# Java JavaScript Swift |
throw |
throw // an exception } finally { |
| Python | raise |
raise # an exception |
| Swift | defer |
defer { |
Exception handling should always be used with any type of processing that is beyond program control.
Key Terms
- exception
- Anomalous or exceptional conditions requiring special processing.[2]
- interrupt
- A request for the processor to interrupt currently executing code (when permitted), so that the event can be processed in a timely manner.[3]