Creating
Your Own Exception Subclasses
Although Java’s built-in
exceptions handle most common errors, you will probably want to create your own
exception types to handle situations specific to your applications. This is
quite easy to do: just define a subclass of Exception (which is, of course, a subclass of Throwable). Your subclasses don’t need to actually implement
anything—it is their existence in
the type system that allows you to use them as exceptions.
The Exception class does not define any methods of its own. It does, of
course, inherit those methods provided by Throwable.
Thus, all exceptions, including those that you create, have the methods defined
by Throwable available to them. They
are shown in Table 10-3. You may also wish to override one or more of these
methods in exception classes that you create.
Exception defines four public constructors. Two support chained exceptions,
described in the next section. The
other two are shown here:
Exception( )
Exception(String msg)
The first form creates an
exception that has no description. The second form lets you specify a
description of the exception.
Although specifying a
description when an exception is created is often useful, sometimes it is
better to override toString( ).
Here’s why: The version of toString( )
defined by Throwable (and inherited
by Exception) first displays the
name of the exception followed by a colon, which is then followed by your
description. By overriding toString( ),
you can prevent the exception name and colon from being displayed. This makes for a cleaner output, which is
desirable in some cases.
The following example
declares a new subclass of Exception
and then uses that subclass to signal an error condition in a method. It
overrides the toString( ) method,
allowing a carefully tailored description of the exception to be displayed.
// This program creates a custom exception type.
class MyException extends Exception {
private int detail;
MyException(int a) { detail = a;
}
public String toString() {
return "MyException[" + detail +
"]";
}
}
class ExceptionDemo {
static void compute(int a) throws MyException {
System.out.println("Called compute(" + a + ")"); if(a >
10)
throw new MyException(a);
System.out.println("Normal exit");
}
public static void main(String args[]) { try {
compute(1);
compute(20);
} catch (MyException e) {
System.out.println("Caught " + e);
}
}
}
This example defines a
subclass of Exception called MyException. This subclass is quite
simple: It has only a constructor plus an overridden toString( ) method that displays the value of the exception. The ExceptionDemo class defines a method
named compute( ) that throws a MyException object. The exception is
thrown when compute( )’s integer
parameter is greater than 10. The main(
) method sets up an exception handler for MyException, then calls
compute( ) with a legal value (less than 10) and an illegal one
to show both paths through
the code. Here is the result:
Called compute(1)
Normal exit
Called compute(20)
Caught MyException[20]
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.