Java Reference
In-Depth Information
a number of different problems, so the extra data (“
/byzero
”) provides more specific in-
formation. Java
does
allow division by zero with floating-point values. Such a calculation
results in the value positive or negative infinity, which is represented in Java as a floating-
point value (but displays as the string
Infinity
or
-Infinity
). If 0.0 is divided by 0.0,
the result is
NaN
(not a number), which is also represented in Java as a floating-point value
(but displays as
NaN
). If you need to compare a floating-point value to
NaN
, use the method
isNaN
of class
Float
(for
float
values) or of class
Double
(for
double
values). Classes
Float
and
Double
are in package
java.lang
.
Starting from the last line of the stack trace, we see that the exception was detected in
line 22 of method
main
. Each line of the stack trace contains the class name and method
(e.g.,
DivideByZeroNoExceptionHandling.main
) followed by the filename and line
number (e.g.,
DivideByZeroNoExceptionHandling.java:22
). Moving up the stack trace,
we see that the exception occurs in line 10, in method
quotient
. The top row of the call
chain indicates the
throw point
—the initial point at which the exception occurred. The
throw point of this exception is in line 10 of method
quotient
.
Stack Trace for an
InputMismatchException
In the third execution, the user enters the string
"hello"
as the denominator. Notice again
that a stack trace is displayed. This informs us that an
InputMismatchException
has oc-
curred (package
java.util
). Our prior examples that input numeric values assumed that
the user would input a proper integer value. However, users sometimes make mistakes and
input noninteger values. An
InputMismatchException
occurs when
Scanner
method
nextInt
receives a
string
that does not represent a valid integer. Starting from the end of
the stack trace, we see that the exception was detected in line 20 of method
main
. Moving
up the stack trace, we see that the exception occurred in method
nextInt
. Notice that in
place of the filename and line number, we're provided with the text
Unknown
Source
. This
means that the so-called
debugging symbols
that provide the filename and line number in-
formation for that method's class were not available to the JVM—this is typically the case
for the classes of the Java API. Many IDEs have access to the Java API source code and will
display filenames and line numbers in stack traces.
Program Termination
In the sample executions of Fig. 11.2 when exceptions occur and stack traces are displayed,
the program also
exits
. This does not always occur in Java. Sometimes a program may con-
tinue even though an exception has occurred and a stack trace has been printed. In such
cases, the application may produce unexpected results. For example, a graphical user in-
terface (GUI) application will often continue executing. In Fig. 11.2 both types of excep-
tions were detected in method
main
. In the next example, we'll see how to
handle
these
exceptions so that you can enable the program to run to normal completion.
InputMismatchExceptions
The application in Fig. 11.3, which is based on Fig. 11.2, uses
exception handling
to pro-
cess any
ArithmeticException
s and
InputMistmatchException
s that arise. The applica-
tion still prompts the user for two integers and passes them to method
quotient
, which
calculates the quotient and returns an
int
result. This version of the application uses ex-