Most of you probably will read this in Saturday morning. However it's still Friday here, so I'll send this out as a Friday Java Quiz.
The Thread.UncaughtExceptionHandler interface in Java 5 allows Java programs to react to Throwables that are not caught and handled by any method on a thread's call stack.
The following class registers a global uncaught exception handler that is invoked when a Throwable bubbles up to the root of any thread stack:
1 public class Main {
2 public static void main(String[] args) {
3 Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
4 public void run() {
5 // Clean up resources: close open files, close databases, etc.6 }
7 }));
8 Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
9 public void uncaughtException(Thread t, Throwable e) {
10 if (e instanceof Error) {
11 System.exit(99);
12 }
13 }
14 });
15 // The application proper: starting the GUI, listening on ServerSockets, etc.16 }
17 }
18
Assume a memory leak in the application causes the JVM to run out of memory and the JVM starts to throw OutOfMemoryError that eventually causes line 11 to be executed and the process is terminated soon afterwards.
Q: What will the exit status of the Java process as seen by the shell be? Is it 99? Can it be anything other than 99?
This is a tricky question. The relaxed rule apply: you can use Google, read books or even compile and run test codes.