Lesson: Exceptions

Explore the concept of exceptions in Java.

Understand what exceptions are and how to use them to handle errors in Java programs.


When running a Java program, if the JVM detects an error it will generate an error condition called an Exception. An Exception is actually an object that contains information about the error and is available for your code to capture and handle as needed. Generating an exception is called throwing, since all Exception objects are subclasses of the Java Throwable object. when an exception occurs, execution of your program stops at that point and the JVM will look for special code that handles exceptions. If an exception is not explicity handled by your code, the JVM will abort your program and report the exception details to the console.

An Exception object identifies the type of exception, indicated by the specific Exception object thrown. There are many pre-defined Exception objects descended from Exception such as IOException or ArithmeticException. An Exception object will contain the location in your program where the exception occurred (stack trace) and may include a description (message).

The most common exception you will encounter is the NullPointerException. This occurs when you attempt to use an object reference variable that has not been set to a valid object reference. Here is an example of this exception in CodingPoint. Compile and run the program to see the exception abort the program and report its information to the console. You can then comment out line 7, compile and run again. This will demonstrate how the stack trace information shows the where in a hierarchy of method calls the exception occurred.

What if you would like to catch exceptions and handle them in some other manner than aborting your program? Java provides a way to do that with try/catch/finally blocks. The general form of a try/catch/finally block is:

This says try executing the code in the try block and if an exception occurs, pass the exception to the catch block, which executes the statements in the catch block (your error handling code). If there is no exception, execution passes to the next statement after the catch block. The code in the optional finally block is always executed exception or not, and execution proceeds to the next statement after the finally block.

Here is an example in CodingPoint of catching an exception. You can compile and run the example and then uncomment the finally block and compile and run again to see how the finally block works. You can also comment out the call to myMethod to see how finally works when there is no exception.

Note that the exception occurred in myMethod but the try/catch block in the main method handled the exception. This is because Java will work its way back through a method call hierarchy until it finds a try/catch block that can handle the exception. Notice we said "finds" a try/catch block that can "handle" the exception. This is because a catch can specify a specific Exception it will handle. If the exception being caught matches an Exception class specified on a catch statement, that catch will process the exception. This is coupled with the fact that you can have multiple catch statements and so tune your exception processing by Exception type.

Here is an example in CodingPoint showing multiple catch statements handling the NullPointerException differently than all other exceptions.

When designing your programs you can use Exceptions for your own error handling. You can trigger exception handling just like Java with the throw statement. You simply throw the exception you want handled:

This will throw the standard Java Exception with your text as it's message.

You can also extend the Exception class to create your own Exceptions. Here is an example in CodingPoint showing how to use exceptions to handle your own error processing. Compile and run to see the generic Java exception used. Then comment the first throw out and uncomment the second. This will show the use of a custom Exception. Finally you can uncomment the catch for the MyException class and see how you can trap custom exceptions.

Note that if you throw exceptions in a method, the throws Exception specifier must be added to the method definition.

Here is a series of videos (video1, video2, video3, video4) about Exceptions and here is a detailed discussion of Exceptions.