Java Reference
In-Depth Information
If a program does not handle the exception at all, it will terminate abnormally
and produce a message that describes what exception occurred and where it was
produced. The information associated with an exception is often helpful in track-
ing down the cause of a problem.
Let's look at the output of an exception. The program shown in Listing 11.1
throws an
ArithmeticException
when an invalid arithmetic operation is attempted.
In this case, the program attempts to divide by zero.
Because there is no code in this program to handle the exception explicitly,
it terminates when the exception occurs, printing specific information about the
exception. Note that the last
println
statement in the program never executes,
because the exception occurs first.
LISTING 11.1
//********************************************************************
// Zero.java Author: Lewis/Loftus
//
// Demonstrates an uncaught exception.
//********************************************************************
public class
Zero
{
//-----------------------------------------------------------------
// Deliberately divides by zero to produce an exception.
//-----------------------------------------------------------------
public static void
main (String[] args)
{
int
numerator = 10;
int
denominator = 0;
System.out.println (numerator / denominator);
System.out.println ("This text will not be printed.");
}
}
OUTPUT
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Zero.main(Zero.java:17)
Search WWH ::
Custom Search