Java Reference
In-Depth Information
This
catch
block looks very much like a method definition that has a parameter of
a type
Exception
. It is not a method definition, but in some ways, a
catch
block is like
a method. It is a separate piece of code that is executed when your program encounters
(and executes) the following (within the preceding
try
block):
throw new
Exception(
Some_String
);
So, this
throw
statement is similar to a method call, but instead of calling a method,
it calls the
catch
block and says to execute the code in the
catch
block. A
catch
block
is often referred to as an
exception handler
.
Let's focus on the identifier
e
in the following line from a
catch
block:
exception
handler
catch
(Exception e)
That identifier
e
in the
catch
block heading is called the
catch
block parameter
.
Each
catch
block can have at most one
catch
block parameter. The
catch
block
parameter does two things:
1. The
catch
block parameter is preceded by an exception class name that specifies what
type of thrown exception object the
catch
block can catch.
2. The
catch
block parameter gives you a name for the thrown object that is caught,
so you can write code in the
catch
block that does things with the thrown object
that is caught.
Although the identifier
e
is often used for the
catch
block parameter, this is not
required. You may use any non-keyword identifier for the
catch
block parameter just
as you can for a method parameter.
catch
block
parameter
catch
Block Parameter
The
catch
block parameter is an identifier in the heading of a
catch
block that serves as a
placeholder for an exception that might be thrown. When a (suitable) exception is thrown in
the preceding
try
block, that exception is plugged in for the
catch
block parameter. The
identifier
e
is often used for
catch
block parameters, but this is not required. You can use any
legal (non-keyword) identifier for a
catch
block parameter.
SYNTAX
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
.