next up previous contents
Next: Java's try-catch blocks Up: Exception handling Previous: Exception handling   Contents


Exception handing

In programming language parlance, an exception is an unexpected event that disrupts the normal execution of a program. Exactly what constitutes an exception can be a subjective decision. For instance, an illegal operation such as division by zero or accessing an array element beyond the bound of the array is almost surely an exception. How about reaching the end of the file when reading a file? This can be viewed as an interruption of the ``normal'' operation of reading a file. Java takes the stand that reaching the end of file is an exceptional situation. Most other programming languages would agree that though this is a special situation, it will generally occur at the end of every read loop and so can be dealt with in the normal program flow (such as checking that the character read is EOF).

The key problem is to deal with exceptions in a sensible way. The aim is for the program to be able to recover and take corrective action if possible and abort only if there is no other option left. Further, it should be possible to report sensible information about the nature of the exception for diagnostic purposes.

In C, a very rudimentary form of exception reporting is done by examining the return value of a function such as main(). Often, a return value of int encodes the result of the computation of the function--minimally, a return value 0 may indicate no error while a non-zero value might indicate an error. The type of error might be indicated by the actual non-zero return value--returning a value 1 might indicate malformed input while returning a value 2 might indicate the inability to open a file that was need for the program to run.



Subsections
next up previous contents
Next: Java's try-catch blocks Up: Exception handling Previous: Exception handling   Contents
Madhavan Mukund 2004-04-29