Exceptions


An exception is an object that defines an unusual or erroneous situation.

Examples

Uncaught Exceptions

If a program does not handle the exception at all, it will terminate abonormally and produce a message that describes what exception occurred and where it was produced.

The output you see is the call stack trace that tells you where the exception occurred. It provides specific information about which methods were called in order to get to the method where the exception occurred.

try/catch

The try/catch statement allows the programmer to deal with exceptions so that they do not cause the program to terminate. The structure of the try/catch statement looks as follows:

try {
	//statements that may throw an exception
} catch(Exception_type name) {
	//do something if an exception of type Exception_type is thrown
} catch(Another_type another_name) {
	//do something if an exception of type Another_type is thrown
} finally {
	//code that will execute whether or not an exception is thrown
}

A try may be followed by only a finally, or simply one or more catch blocks, or one or more catch blocks followed by a finally. The code in the finally block executes regardless of whether an exception is thrown. A good use of the finally block is to ensure that a file is closed once its contents have been read.

Propagation

If a method contains a line of code that may throw an exception, the code may be placed in a try block, or the method header may declare that the method propagates the exception. This means that if an exception occurs, it will be passed along to the caller of the method. The following code demonstrates a method that propagates am exception to its caller.

public void processFile(String filename) throws IOException {
	//code to process a file
	//if the code generates an exception, the exception will be thrown to the caller of processFile
}

The caller of this method must then also propagate the exception, or place the call to the method in a try block.

Exception Class Hierarchy

Checked and Unchecked Exceptions

You may have noticed that for some types of exceptions the compiler complains if you do not either propgate them or catch them whereas other types of exceptions appear at runtime without any warning. Any exception that derives from RuntimeException is an unchecked exception. Unchecked exceptions need not be caught or propagated. Any exception not derived from RuntimeException is a checked exception and must be caught or propagated.

Many people think that checked excceptions were a mistake. See the following web pages for a discussion of checked exceptions.

http://www.mindview.net/Etc/Discussions/CheckedExceptions

http://www.ibm.com/developerworks/java/library/j-jtp05254.html

Defining Your Own Exceptions

To create your own exceptions, you merely need to define a class that extends Exception (or RuntimeException if you want to define an unchecked exception). Your exception can then be used like any other exception. Also, remember that an exception is an object like everything else in Java. To create and throw a new exception object you simply use the new operator to create an instance of your exception class and use the keyword throw to throw your exception to the calling method as follows:

if(num < 0 || num > 10) {
	throw new OutOfRangeException("Your number is out of range.");
}


Sami Rollins

Date: 2007-09-13