|
JavaFAQ Home » Java Lectures by Anatoliy Malyarenko

Exceptions I
by: Anatoliy Malyarenko
Abstract
Contents of the lecture.
- What's an exception and why do I care?
- First encounter with Java exceptions.
- Java's catch or specify requirement.
- Dealing with exceptions.
Introduction
If there's a golden rule of programming it's this: Errors occur in software programs.
This we know. But what really matters is what happens after the error occurs. How is the error
handled? Who handles it? Can the program recover, or should it just die?
What's an exception and why do I care?
The term exception is shorthand for the phrase "exceptional event." It can be defined as
follows:
Definition 1. An exception is an event that occurs during the execution of a program that
disrupts the normal flow of instructions.
Many kinds of errors can cause exceptions -- problems ranging from serious hardware
errors, such as a hard disk crash, to simple programming errors, such as trying to access an
out-of-bounds array element. When such an error occurs within a Java method, the method
creates an exception object and hands it off to the runtime system. The exception object
contains information about the exception, including its type and the state of the program when
the error occurred. The runtime system is then responsible for finding some code to handle the
error. In Java terminology, creating an exception object and handing it to the runtime system
is called throwing an exception.
After a method throws an exception, the runtime system leaps into action to find
someone to handle the exception. The set of possible "someones" to handle the exception
is the set of methods in the call stack of the method where the error occurred. The runtime
system searches backwards through the call stack, beginning with the method in which the
error occurred, until it finds a method that contains an appropriate exception handler. An
exception handler is considered appropriate if the type of the exception thrown is the same
as the type of exception handled by the handler. Thus the exception bubbles up through the
call stack until an appropriate handler is found and one of the calling methods handles the
exception. The exception handler chosen is said to catch the exception.
If the runtime system exhaustively searches all of the methods on the call stack without
finding an appropriate exception handler, the runtime system (and consequently the Java
program) terminates.
By using exceptions to manage errors, Java programs have the advantages over
traditional error management techniques.
Advantage 1: separating error handling code from "regular" code
In traditional programming, error detection, reporting, and handling often lead to
confusing spaghetti code. For example, suppose that you have a function that reads an entire
file into memory. In pseudo-code, your function might look something like this:
| Code: |
readFile {
open the file;
determine its size;
allocate that much memory;
read the file into memory;
close the file;
}
|
At first glance this function seems simple enough, but it ignores all of these potential
errors:
- What happens if the file can't be opened?
- What happens if the length of the file can't be determined?
- What happens if enough memory can't be allocated?
- What happens if the read fails?
- What happens if the file can't be closed?
To answer these questions within your readFile function, you'd have to add a lot of code
to do error detection, reporting and handling. Your function would end up looking something
like this:
| Code: |
errorCodeType readFile {
initialize errorCode = 0;
open the file;
if (theFileIsOpen) {
determine the length of the file;
if (gotTheFileLength) {
allocate that much memory;
if (gotEnoughMemory) {
read the file into memory;
if (readFailed) {
errorCode = -1;
}
} else {
errorCode = -2;
}
} else {
errorCode = -3;
}
close the file;
if (theFileDidntClose && errorCode == 0) {
errorCode = -4;
} else {
errorCode = errorCode and -4;
}
} else {
errorCode = -5;
}
return errorCode;
}
|
With error detection built in, your original 7 lines (in bold) have been inflated to 29 lines of
code -- a bloat factor of almost 400 percent. Worse, there's so much error detection, reporting,
and returning that the original 7 lines of code are lost in the clutter. And worse yet, the logical
flow of the code has also been lost in the clutter, making it difficult to tell if the code is doing
the right thing: Is the file really being closed if the function fails to allocate enough memory?
It's even more difficult to ensure that the code continues to do the right thing after you modify
the function three months after writing it. Many programmers "solve" this problem by simply
ignoring it -- errors are "reported" when their programs crash.
Java provides an elegant solution to the problem of error management: exceptions.
Exceptions enable you to write the main flow of your code and deal with the, well, exceptional
cases elsewhere.
If your readFile function used exceptions instead of traditional error
management techniques, it would look something like this:
| Code: |
readFile {
try {
open the file;
determine its size;
allocate that much memory;
read the file into memory;
close the file;
} catch (fileOpenFailed) {
doSomething;
} catch (sizeDeterminationFailed) {
doSomething;
} catch (memoryAllocationFailed) {
doSomething;
} catch (readFailed) {
doSomething;
} catch (fileCloseFailed) {
doSomething;
}
}
|
Note that exceptions don't spare you the effort of doing the work of detecting, reporting,
and handling errors. What exceptions do provide for you is the means to separate all the
grungy details of what to do when something out-of-the-ordinary happens from the main logic
of your program.
In addition, the bloat factor for error management code in this program is about 250
percent -- compared to 400 percent in the previous example.
Advantage 2: propagating errors up the call stack
A second advantage of exceptions is the ability to propagate error reporting up the
call stack of methods. Suppose that the readFile method is the fourth method in a series
of nested method calls made by your main program: method1 calls method2, which calls
method3, which finally calls eadFile.
| Code: |
method1 {
call method2;
} method2 {
call method3;
} method3 {
call readFile;
}
|
Suppose also that method1 is the only method interested in the errors that occur within
readFile. Traditional error notification techniques force method2 and method3 to propagate
the error codes returned by readFile up the call stack until the error codes finally reach
method1 -- the only method that is interested in them.
| Code: |
method1 {
errorCodeType error = call method2;
if (error)
doErrorProcessing;
else
proceed;
}
errorCodeType method2 {
errorCodeType error = call method3;
if (error)
return error;
else
proceed;
}
errorCodeType method3 {
errorCodeType error = call readFile;
if (error)
return error;
else
proceed;
}
|
As you learned earlier, the Java runtime system searches backwards through the call
stack to find any methods that are interested in handling a particular exception. A Java method
can "duck" any exceptions thrown within it, thereby allowing a method further up the call stack
to catch it. Thus only the methods that care about errors have to worry about detecting errors.
| Code: |
method1 {
try {
call method2;
} catch (exception) {
doErrorProcessing;
}
}
method2 throws exception {
call method3;
}
method3 throws exception {
call readFile;
}
|
However, as you can see from the pseudo-code, ducking an exception does require
some effort on the part of the "middleman" methods. Any checked exceptions that can be
thrown within a method are part of that method's public programming interface and must be
specified in the throws clause of the method. Thus a method informs its callers about the
exceptions that it can throw, so that the callers can intelligently and consciously decide what
to do about those exceptions.
Note again the difference in the bloat factor and code obfuscation factor of these two
error management techniques. The code that uses exceptions is more compact and easier to
understand. Printer Friendly Page
Send to a Friend
..
Search here again if you need more info!
|