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
try
catch
finally
throw // an exception
try {
// code statements
} catch {
// handle exception
} finally {
// clean up
}
Python raise
try
except
finally
raise # an exception
try:
# code statements
except:
# handle exception
finally:
# clean up
Swift defer
throw
do try
catch
defer {
// clean up
}
throw // an exception
do {
try // code statements
} catch {
// handle exception
}

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]

References


License

Icon for the Creative Commons Attribution-ShareAlike 4.0 International License

Programming Fundamentals Copyright © 2018 by Authors and Contributors is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License, except where otherwise noted.

Share This Book