Java Reference
In-Depth Information
6.
method1
catches the exception on line 6. The
catch
block executes and the stack trace
is printed. Flow of control now continues normally for the remainder of this program.
7.
method1
finishes executing normally, so control jumps back to
main
and
End of main
displays. The program successfully runs to completion.
Here is the output of
CheckedDemo
when a
ClassNotFoundException
occurs:
Inside main
Inside method1
Inside method2
Inside method3
java.lang.ClassNotFoundException: java.lang.string
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at CheckedDemo.method3(CheckedDemo.java:18)
at CheckedDemo.method2(CheckedDemo.java:13)
at CheckedDemo.method1(CheckedDemo.java:5)
at CheckedDemo.main(CheckedDemo.java:24)
End of main
Exceptions on the Exam
Watch for questions on the exam that specifi cally test your knowledge of the Handle or
Declare Rule. Remember that declaring a checked exception does not magically take care
of the exception. It simply pushes the responsibility of handling that exception to some
other method, which means somewhere down the call stack a
try
statement is needed to
handle the exception and the compiler enforces this rule.
You should also know which common exceptions are runtime or checked exceptions.
For example, you should know that
ArithmeticException
and
NullPointerException
are runtime exceptions, while
IOException
,
ClassNotFoundException
, and
InterruptedException
are checked exceptions.
Search WWH ::
Custom Search