Java Reference
In-Depth Information
at the time, and this can be helpful. But it contains a bit more. To illustrate, we
change the program so that the division by
0
occurs within a different method:
public class
Ex {
public static void
main(String[] args)
{ first(); }
public static void
first()
{ second(); }
public static void
second()
{ System.out.println( 5 / 0 ); }
}
Suppose method
main
is called. Method
main
calls
first
, which calls
sec-
ond
, which divides by
0
. The list of calls that have been started but have not com-
pleted is called the
call stack
. When the division by zero occurs, the same excep-
tion is thrown, and the following appears in the Java console:
java.lang.ArithmeticException: / by zero
at Ex.second(Ex.java:7)
at Ex.first(Ex.java:5)
at Ex.main(Ex.java:3)
As before, the first line says that an
ArithmeticException
occurred, which
was a division by zero. The second line says that the exception occurred in
sec-
ond
, at line 7 of file
Ex.java
. The third line says that
second
was called from
first
, and the fourth line says that
first
was called from
main
.
Thus, when an exception occurs:
A message on the Java console describes the call stack: the stack
of methods that have been called but have not yet completed.
You can use this stack of calls to help figure out how your program got to the
point of throwing the exception.
If your program aborts with one of these errors, there is a severe problem:
OutOfMemoryError
InternalError
UnknownError
In the first case, you have to find out why your program used too much memo-
ry. In the other two cases, it is difficult to say what to do. Something caused
things to become really messed up. Perhaps recompiling all files may help.
Search WWH ::
Custom Search