Java Reference
In-Depth Information
catch
(
Exception_Class_Name
Catch_Block_Parameter
)
{
<
Code to be performed if an exception of the named exception
class is thrown in the
try
block.
>
}
You may use any legal identifier for the
Catch_Block_Parameter
; a common choice is
e
. The
code in the
catch
block may refer to the
Catch_Block_Parameter
. If there is an explicit
throw
statement, it is usually embedded in an
if
statement or an
if-else
statement.
There may be any number of
throw
statements and/or any number of method invocations
that may throw exceptions. Each
catch
block can list only one exception, but there can be
more than one
catch
block.
EXAMPLE
See Display 9.2.
Exception Classes from Standard Packages
Numerous predefined exception classes are included in the standard packages that come
with Java. The names of predefined exceptions are designed to be self-explanatory.
Some sample predefined exceptions are
IOException
NoSuchMethodException
FileNotFoundException
The predefined exception class
Exception
is the root class for all exceptions. Every
exception class is a descendent of the class
Exception
(that is, it is derived directly
from the class
Exception
or from a class that is derived from the class
Exception
, or it
arises from some longer chain of derivations ultimately starting with the class
Exception
). You can use the class
Exception
itself, just as we did in Display 9.2, but
you are even more likely to use it to define a derived class of the class
Exception
. The
class
Exception
is in the
java.lang
package and so requires no import statement.
Exception
The Class
Exception
Every exception class is a descendent class of the class
Exception
. You can use the class
Exception
itself in a class or program, but you are even more likely to use it to define a
derived class
of the class
Exception
. The class
Exception
is in the
java.lang
package and
so requires no import statement.