Exception Handling

What is an exception?

An exception is a notice that a certain condition exists or that a certain event has occurred. These are things which an application might want to try to deal with. Examples include attempts to divide by zero (Java's ArithmeticException class) and the detection of the end-of-file condition (Java's EOFException class). In these situations, an object of type Exception (or one of its subtypes) is created and thrown.

What can I do if an exception is throw?

Some errors (like the IndexOutOfBounds exception) may be situations that your program has no recipe to remedy. Java more or less expects your program to concede defeat if these situations occur, so the compiler doesn't do any checking to see if you are ready for them. Other types of exceptions are checked exceptions and the Java compiler forces you to specify how you will deal with them if they occur.

For these checked exception types, Java gives you two choices and requires you to pick one of them. You are welcome to specify one of these choices for unchecked exceptions too … if you think you have a way to deal with them or recover from them in your program.

You can throw the exception to the next higher level (i.e., back to the method that called this one … or in the case of the main() method, back to the system). If you throw the exception in this manner, your method will terminate when the exception occurs.

Alternatively, you can handle the exception yourself. This gives you the opportunity to deal with the circumstances that lead to the exception. You may be able to successfully deal with the situation and let your method continue to execute.

How can I know which methods can throw exceptions?

Read the method description in the Java API. The method descriptions in all the Java API classes include a Throws: section which lists each type of exception that can be thrown, and describes the circumstances under which it is thrown.

If one of your own methods throws an exception, it will contain a throw statement (which you would have put there yourself). This should be enough to make you aware of the possibility of an exception being thrown.

When should I handle an exception myself, rather than throwing it to the next higher level?

You'd better know what you want to do in the way of handling the exception. Either you want to save some precious data before crashing … or you want to try to actually recover from the error and continue execution of your program. [In fact, some exceptions aren't errors at all, they are simply events (like end of file) that a program expects to be notified of.] If you don't have any ideas about how to do either of these things, you'd better just throw the exception to the next higher level.

Can I create and throw my own types of exceptions?

Yes. You can create a new type of exception by extending the Exception class (or any of its subclasses). When you invoke the constructor of this class to create an instance, you would normally pass an error message (a String) to the constructor.

What does an instance of the Exception class look like?

If e is an exception, then System.out.println(e); invokes the object's toString() method to create a printable message. In effect, an exception is simply an error message with a notification capability.



Things to try
  1. Write a program that declares an array and then does something that lets a subscript go out of bounds. Put the code that accesses the array in a try block and code a catch block to catch and handle the IndexOutOfBoundsException when it occurs. Your catch block code can do nothing (just ignore the error, but print a message to let you know the exception was caught) and let your program terminate normally.

© Wiggen & Associates, 2007