Java Reference
In-Depth Information
TABLE 11-2
class
Exception
and its Constructors
public
Exception()
//Default constructor
//Creates a new instance of the class Exception
public
Exception(String str)
//Constructor with parameters
//Creates a new instance of the class Exception. The parameter
//str specifies the message string.
Because the
class
Exception
is a subclass of the
class
Throwable
, the
class
Exception
and its subclasses inherit the methods
getMessage
,
printStackTrace
,
and
toString
. The method
getMessage
returns the string containing the detailed
message stored in the exception object. The method
toString
returns the detailed
message stored in the exception object as well as the name of the exception class. The
method
printStackTrace
is discussed later in this chapter.
The
class
RuntimeException
is the superclass of the classes designed to deal with excep-
tions, such as division by zero, array index out of bounds, and number format (see Figure 11-2).
Table 11-3 lists some of the exception classes and the type of exceptions they throw.
TABLE 11-3
Some of Java's Exception Classes
Exception Class
Description
Arithmetic errors such as division by
zero
ArithmeticException
1
1
Array index is either less than 0 or
greater than or equal to the length of
the array.
ArrayIndexOutOfBoundsException
Reference to a file that cannot be
found
FileNotFoundException
Calling a method with illegal
arguments
IllegalArgumentException
An array or a string index is out of
bounds.
IndexOutOfBoundsException
Reference to an object that has not
been instantiated
NullPointerException
Search WWH ::
Custom Search