Java Reference
In-Depth Information
Enter two integers:
Exception: an integer cannot be divided by zero
Execution continues ...
5 0
If
number2
is
0
, the method throws an exception (line 6) by executing
throw new
ArithmeticException(
"Divisor cannot be zero"
);
throw
statement
The value thrown, in this case
new ArithmeticException("Divisor cannot be zero")
,
is called an
exception
. The execution of a
throw
statement is called
throwing an exception
. The
exception is an object created from an exception class. In this case, the exception class is
java.lang.ArithmeticException
. The constructor
ArithmeticException(str)
is
invoked to construct an exception object, where
str
is a message that describes the exception.
When an exception is thrown, the normal execution flow is interrupted. As the name sug-
gests, to “throw an exception” is to pass the exception from one place to another. The state-
ment for invoking the method is contained in a
try
block and a
catch
block. The
try
block
(lines 19-23) contains the code that is executed in normal circumstances. The exception is
caught by the
catch
block. The code in the
catch
block is executed to
handle the exception
.
Afterward, the statement (line 29) after the
catch
block is executed.
The
throw
statement is analogous to a method call, but instead of calling a method, it calls
a
catch
block. In this sense, a
catch
block is like a method definition with a parameter that
matches the type of the value being thrown. Unlike a method, however, after the
catch
block
is executed, the program control does not return to the
throw
statement; instead, it executes
the next statement after the
catch
block.
The identifier
ex
in the
catch
-block header
exception
throwing exception
handle exception
catch
(ArithmeticException ex)
acts very much like a parameter in a method. Thus, this parameter is referred to as a
catch
-block parameter. The type (e.g.,
ArithmeticException
) preceding
ex
specifies
what kind of exception the
catch
block can catch. Once the exception is caught, you can
access the thrown value from this parameter in the body of a
catch
block.
In summary, a template for a
try
-
throw
-
catch
block may look like this:
catch
-block parameter
try
{
Code to run;
A statement or a method that may throw an exception;
More code to run;
}
catch
(type ex) {
Code to process the exception;
}
An exception may be thrown directly by using a
throw
statement in a
try
block, or by invok-
ing a method that may throw an exception.
The main method invokes
quotient
(line 20). If the quotient method executes normally,
it returns a value to the caller. If the
quotient
method encounters an exception, it throws the
exception back to its caller. The caller's
catch
block handles the exception.
Now you can see the
advantage
of using exception handling: It enables a method to throw
an exception to its caller, enabling the caller to handle the exception. Without this capability,
the called method itself must handle the exception or terminate the program. Often the called
method does not know what to do in case of error. This is typically the case for the library
methods. The library method can detect the error, but only the caller knows what needs to be
advantage