Java Reference
In-Depth Information
5. Because method3 does not catch the exception, it is immediately popped off the call
stack. Notice that line 16 does not execute.
6. Because method2 does not catch the exception, it also pops off the call stack. The same
happens with method1 and main .
7. Because the exception was not caught, the program terminates and the JVM dumps the
following stack trace:
Inside main
Inside method1
Inside method2
Inside method3
Exception in thread “main” java.lang.ArithmeticException: / by zero
at ExceptionDemo.method3(ExceptionDemo.java:15)
at ExceptionDemo.method2(ExceptionDemo.java:9)
at ExceptionDemo.method1(ExceptionDemo.java:4)
at ExceptionDemo.main(ExceptionDemo.java:21)
Figure 3.9 shows the exception being thrown down the method call stack of ExceptionDemo .
FIGURE 3.9 The ArithmeticException is thrown down the call stack.
method3 causes an ArithmeticException
to be thrown down the call stack.
ArithmeticException
object
method3
method2
method1
main
Notice that an unhandled exception terminates your program, which obviously is not
good if you don't want your program terminating every time an exception occurs.
We now look at how to catch an exception so it does not terminate the application using
a try statement.
Search WWH ::




Custom Search