Java Reference
In-Depth Information
message.toUpperCase();
System.out.println(“End of try”);
}finally {
System.out.println(“Inside finally”);
}
System.out.println(“End of go”);
return message;
}
public static void main(String [] args) {
System.out.println(“Inside main”);
TryFinally test = new TryFinally();
System.out.println(test.go());
System.out.println(“End of main”);
}
}
The statement
message.toUpperCase()
throws a
NullPointerException
. Here is the
output of running the code:
Inside main
Inside go
Inside finally
Exception in thread “main” java.lang.NullPointerException
at TryFinally.go(TryFinally.java:6)
at TryFinally.main(TryFinally.java:18)
Even though an uncaught exception is thrown within the
try
block, the
finally
block
still executes before the exception is actually thrown and
go
is popped off the method call
stack.
There are situations where a
finally
block might not execute. For example, if an error
is thrown and the JVM is no longer able to run properly, a
finally
block probably might
not execute. Calling
System.exit
in a
catch
block terminates the JVM, which means the
corresponding
finally
block cannot execute.
Now that we have discussed the details and syntax of handling and declaring
exceptions, I want to go over some of the common types of exceptions and errors that
arise in Java. The following section discusses the details of various exceptions and errors
specifi cally listed in the certifi cation exam objectives.
Search WWH ::
Custom Search