Main Page

Previous Next

The Idea Behind Exceptions

An exception usually signals an error, and is so-called because errors in your Java programs are bound to be the exception rather than the rule - by definition! An exception doesn't always indicate an error though - it can also signal some particularly unusual event in your program that deserves special attention.

If, in the midst of the code that deals with the normal operation of the program, you try to deal with the myriad, and often highly unusual, error conditions that might arise, your program structure will soon become very complicated and difficult to understand. One major benefit of having an error signaled by an exception is that it separates the code that deals with errors from the code that is executed when things are moving along smoothly. Another positive aspect of exceptions is that they provide a way of enforcing a response to particular errors - with many kinds of exceptions, you must include code in your program to deal with them, otherwise your code will not compile.

One important idea to grasp is that not all errors in your programs need to be signaled by exceptions. Exceptions should be reserved for the unusual or catastrophic situations that can arise. A user entering incorrect input to your program for instance is a normal event, and should be handled without recourse to exceptions. The reason for this is that dealing with exceptions involves quite a lot of processing overhead, so if your program is handling exceptions a lot of the time it will be a lot slower than it needs to be.

An exception in Java is an object that's created when an abnormal situation arises in your program. This exception object has data members that store information about the nature of the problem. The exception is said to be thrown, that is, the object identifying the exceptional circumstance is tossed, as an argument, to a specific piece of program code that has been written specifically to deal with that kind of problem. The code receiving the exception object as a parameter is said to catch it.

The situations that cause exceptions are quite diverse, but they fall into four broad categories:

Code or Data Errors

For example, you attempt an invalid cast of an object, you try to use an array index that's outside the limits for the array, or an integer arithmetic expression that has a zero divisor.

Standard Method Exceptions

For example, if you use the substring() method in the String class, it can throw a StringIndexOutOfBoundsException exception.

Throwing your own Exceptions

We'll see later in this chapter how you can throw a few of your own when you need to.

Java Errors

These can be due to errors in executing the Java Virtual Machine which runs your compiled program, but usually arise as a consequence of an error in your program.

Before we look at how you make provision in your programs for dealing with exceptions, we should understand what specific classes of exceptions could arise.

Previous Next
JavaScript Editor Java Tutorials Free JavaScript Editor