img
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.
Method
Description
Throwable fillInStackTrace( )
Returns a Throwable object that contains a completed
stack trace. This object can be rethrown.
Throwable getCause( )
Returns the exception that underlies the current
exception. If there is no underlying exception, null
is returned.
String getLocalizedMessage( )
Returns a localized description of the exception.
String getMessage( )
Returns a description of the exception.
StackTraceElement[ ] getStackTrace( )
Returns an array that contains the stack trace, one
element at a time, as an array of StackTraceElement.
The method at the top of the stack is the last method
called before the exception was thrown. This method
is found in the first element of the array. The
StackTraceElement class gives your program access
to information about each element in the trace, such
as its method name.
Associates causeExc with the invoking exception as a
Throwable initCause(Throwable
causeExc)
cause of the invoking exception. Returns a reference
to the exception.
void printStackTrace( )
Displays the stack trace.
Sends the stack trace to the specified stream.
void printStackTrace(PrintStream
stream)
Sends the stack trace to the specified stream.
void printStackTrace(PrintWriter
stream)
void setStackTrace(StackTraceElement
Sets the stack trace to the elements passed in
elements[ ]) elements. This method is for specialized applications,
not normal use.
String toString( )
Returns a String object containing a description of the
exception. This method is called by println( ) when
outputting a Throwable object.
TABLE 10-3
The Methods Defined by Throwable
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home