A Bit About Exception Handling


Introduction.  One of the things I kept trying to illustrate in our earlier programs was how they terminated if even the tiniest little thing wasn't done correctly.  We all know how this sort of thing can be a problem when working with computers.  But, on the other hand, how is a user supposed to know what the did wrong? 

Exception handling is the most polite term used to describe efforts at coding to deal with things that don't go exactly as planned when interacting with end users.  Other terms used by computer people are error handling and error trapping.

The idea is to write code so that if the user doesn't do things as desired then the program doesn't just blow up.  Hopefully the program maintains functionality and gives the user feedback about how they should improve their interaction.

Java has some built in classes, Exception in particular, and methods to help developers bring about such lofty goals.  This is a topic we will delve into throughout our two semesters of Java.  This webpage will present an overall summary of many of the options for exception handling that are available in Java.  In future webpages we will focus on a few of them at a time and get into more depth.

The following diagram represents some of the subclasses of the class Exception that are included in the package java.lang.



Unfortunately, I have seen far too many of these exceptions when developing my own code.  You are likely to get to know them all too well as you work in Java.

The following tables will overview a variety of these so we can refer back to them in future webpages.  First, we will give brief descriptions about some of the above classes.


Exception Class Description
Arithmetic Exception arithmetic errors such as division by zero
ArrayIndexOutOfBoundsException an array index is either less than zero or greater than or equal to the declared upper limit
FileNotFoundException reference to a file cannot be found
IllegalArgumentException calling a method with illegal arguments
IndexOutOfBoundsException an array index is out of bounds
NullPointerException reference to an object that has not been instantiated
NumberFormatException use of an illegal number format
StringIndexOutOfBoundsException a string index is either less than zero or greater than or equal to the length of the string


We will actually see more than these and develop some of our own classes to handle some others during this course.

Try/Catch/Finally Blocks.  Now we need to discuss a couple of the major aspects of the overall structure used to deal with exception handling in Java.  We won't see any specific examples until the next webpage, but this discussion will provide some fundamental and recurring structure when exception handling.

Statements that might generate an exception that we are trying to handle are placed in a try block.  If an exception occurs within the block of code we are trying then program control attempts to shift to a catch block.  The catch block must specify the type of exception it is intended to handle.  There may also be more than one catch block.  Too often, there are none.

In the end, code that should execute regardless of whether an exception occurs is placed in a finally block.  If a try block has no catch blocks then it must have a finally block.

The basic structure associated with try and catch blocks is given in the following table.



code statements


catch (ExceptionClassName1  reference1)

code to handle this sort of exception


catch (ExceptionClassName2  reference2)

code to handle this sort of exception


 . . .

catch (ExceptionClassNameN  referenceN)

code to handle this sort of exception



code that executes regardless



Several rules are important to know
  • if no exception occurs, or is thrown, all of the catch blocks associated with the try block are ignored and execution resumes after the last catch block
  • when an exception is thrown in a try block all the rest of statements in the try block are ignored
    • the program looks through the catch blocks that follow in order to find the first one that has an ExceptionClassName that is most appropriate for the exception
    • the program chooses the first catch block it finds with an appropriate ExceptionClassName and it does the exception handling
    • the remaining catch blocks are ignored
  • if there is a finally block the code within it executes regardless of whether an exception occurs

This is enough of our probably far too abstract presentation about try and catch blocks.  The next web page will develop code using try and catch blocks to deal with the difficulties we were encountering with our inputs in the message box programs.