<< That Was Blog Post 1024 | Home | Friday Java Quiz: What Will The GUI Show? >>

Friday Java Quiz: What's The Exit Status

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.

Tags :


Re: Friday Java Quiz: What's The Exit Status

You give a clue by including the ShutdownHook (which is executed after the System.exit and therefore after the UncaughtExceptionHandler in your example.) As this is triggered by the call to system.exit it is possible to interfere with the call by calling Runtime halt. This method takes a status code and therefore can override 99

Re: Friday Java Quiz: What's The Exit Status

Indeed.

However, that's not what I have in mind. My shutdown hook only perform resource cleanup as the comment indicates.

Re: Friday Java Quiz: What's The Exit Status

If the shutdown hook's run() method does not throw any Error then the status will be 99. However if there is an Error thrown from the run method of the shutdown hook then it will block indefinitely. For example, if at line 5, a java.lang.AssertionError is thrown then the JVM does not exit.

Re: Friday Java Quiz: What's The Exit Status

If an Error was thrown in line 5, the uncaught exception handler will kick in again on the shut down hook thread, which calls System.exit(99) again. This causes an indefinite block. Calling System.exit(42) directly in the shut down hook will cause the same indefinite block.

However, in my case, the JVM process did terminate.

Re: Friday Java Quiz: What's The Exit Status

At line 5, if we execute the following then the status will be different.

Runtime.getRuntime().halt(40);

In this example we can change the status from 99 to 40.

Add a comment Send a TrackBack