Java Reference
In-Depth Information
• You want to add information when a standard exception occurs, and you can do this by rethrowing
an object of your own exception class.
• You may have error conditions that arise in your code that warrant the distinction of a special ex-
ception class.
• To consolidate exceptions into a smaller, more manageable set (making it easier on calling meth-
ods).
However, you should bear in mind that there's a lot of overhead in throwing exceptions, so it is not a
valid substitute for “normal” recovery code that you would expect to be executed frequently. If you have
recovery code that is executed often, then it doesn't belong in a catch block, but rather in something like an
if-else statement.
Let's see how you create your own exceptions.
Defining an Exception Class
Your exception classes must always have Throwable as a superclass; otherwise, they do not define an ex-
ception. Although you can derive them from any of the standard exception classes, your best policy is to
derive them from the Exception class or from a subclass of Exception . This allows the compiler to keep
track of where such exceptions are thrown in your program and checks that they are either caught or de-
clared as thrown in a method. If you use RuntimeException or one of its subclasses, the compiler checking
for catch blocks of your exception class are suppressed.
Let's go through an example of how you define an exception class:
public class DreadfulProblemException extends Exception {
// Constructors
public DreadfulProblemException(){ } // Default constructor
public DreadfulProblemException(String s) {
super(s); // Call the base class constructor
}
}
This is the minimum you should supply in your exception class definition. By convention, your exception
class should include a default constructor and a constructor that accepts a String object as an argument. The
message stored in the superclass Exception (in fact, in Throwable , which is the superclass of Exception )
is automatically initialized with the name of your class, whichever constructor for your class objects is used.
The String passed to the second constructor is appended to the name of the class to form the message stored
in the exception object. Of course, you can subclass any of the classes derived from Exception .
You can add other constructors in your exception class. In general, you want to do so, particularly when
you're rethrowing your own exception after a standard exception has been thrown because you are likely
to want additional parameters for passing information relating to your exceptions. In addition, you typically
want to add instance variables to the exception class that store additional information about the problem,
plus methods that enable the code in a catch block to get at the data. Because your exception class is ulti-
mately derived from Throwable , the stack trace information is automatically available for your exceptions
along with the capability for creating chained exceptions. If you plan to use exception chaining, you will
want to define at least one constructor with a Throwable parameter so that a reference to a previous excep-
tion in a chain can be passed. In this case the class constructors could be:
Search WWH ::




Custom Search