views:

69

answers:

2

When i execute the below code,I am seeing the output as:

Finally

Exception in thread "main" java.lang.NullPointerException at ClientTestConcepts.main(ClientTestConcepts.java:9)

Who prints the bold faced statements.

public class ClientTestConcepts {
    public static void main(String []args){
        try{
            throw new NullPointerException();
        }
        finally{
            System.out.println("Finally");
        }
    }
}
+7  A: 

The Java runtime.

It catches all exceptions not handled in user code, and prints them on the error output (by default).

Péter Török
What happen if I catch it?
JavaUser
@JavaUser, whatever you do in the `catch` block :-) If you catch it within `main` and do not rethrow it, the runtime will not see it.
Péter Török
+3  A: 

Each thread has a default uncaught exception handler that runs when an exception makes it to the top of the stack. The one you are observing is provided by ThreadGroup.uncaughtException:

Called by the Java Virtual Machine when a thread in this thread group stops because of an uncaught exception, and the thread does not have a specific Thread.UncaughtExceptionHandler installed.

The uncaughtException method of ThreadGroup does the following: (...)
... a message containing the thread's name, as returned from the thread's getName method, and a stack backtrace, using the Throwable's printStackTrace method, is printed to the standard error stream.

If you want some other behaviour register an uncaught exception handler for the thread.

Mark Peters
Using ThreadGroup is obsolete.
Bozho
First of all, it's not the user that's using ThreadGroup. Second of all, that's what's happening. Whether it's obsolete or not (I would say it's not, since it's still very much used), it's what's causing the user behaviour now. Not some magic in the Java runtime.
Mark Peters
BTW if anybody knows how to fix that link, I'd be delighted. It's interpreted correctly in the preview pane so I think it's a SO bug.
Mark Peters
Well, the very quote says _"Called by the Java Virtual Machine"_ ... so it may not be magic, but it **is** caused by the Java runtime :-)
Péter Török
@Peter: Called, yes. But it's not like it's printed in some native code. It's fully Java code provided in the runtime library that actually does the printing. And it's configurable.
Mark Peters
I managed to fix the link by replacing the ' ' (space) in it with `%20` :-)
Péter Török
I don't think _Java runtime_ is supposed to mean _native code_ (but I may be wrong of course). And your info about its configurability is useful, +1 for you :-)
Péter Török
Thanks, @Peter! I'll keep that in mind for future reference. Interesting that it didn't escape it for me, given that I used the editor button to insert the link in the first place.
Mark Peters
@Peter, I of course agree about the Java runtime, though clearly anything that happens at runtime not by user code is happening because of the Java runtime. So I was trying to be a bit more explicit.
Mark Peters
Yeah, the editor is not very good regarding unusual links (containing spaces or brackets, among others). But also it is the cleverness of modern browsers. They used to show the links with the escape characters included, but now that I checked it, Firefox at least shows the space.
Péter Török
@Peter: Thanks, that's the reason it happened. I found a better way to copy a link from Vimperator.
Mark Peters