1

Stack trace is as follows:

c b a main 

Assume that c is throwing FileNotFoundException. I propagated this exception to b using throws. And I am propagating exceptions from b to a and from a to main.

When I use throws FileNotFoundException in main, where will the exception be propagated?

Because I didn't define catch inside main for FileNotFoundException, but only throws.

3 Answers 3

3

To the Java runtime, which will print the stack trace and abort the program.

(I'm curious what led to this question. Did you try the code? Where you confused by its behavior?)

Sign up to request clarification or add additional context in comments.

5 Comments

I have read somewhere like calling method should handle exception when called method throws some exception.but in calling method I just declared exception.My doubt is declaration itself is enough to handle exception.
Well, the calling method "handles" the exception by saying "nope, not touching this, whoever called me has to deal". Yes, that's enough. Whether it's the right thing to do is extremely situation-dependent.
Thanks,Sebastian.So exception will be handled by JVM when we use throws on main method.
Yes as I said: it will print the stack trace and abort the program. (Well, as Joni correctly points out, it just calls the uncaught exception handler, which does that.) That's handling. You seem to attach some special significance to "handling" an exception, like it's necessary to call a special function to say, "this exception is now handled". You don't. The moment you catch an exception and don't rethrow it, it's handled.
Declaring a throws clause doesn't mean that the method doesn't catch the exception.
3

An uncaught exception will eventually be handled by the UncaughtExceptionHandler of the thread that is executing the code. The Thread class has methods for setting the handler for uncaught exceptions. The documentation of Thread#setDefaultUncaughtExceptionHandler explains the process:

Uncaught exception handling is controlled first by the thread, then by the thread's ThreadGroup object and finally by the default uncaught exception handler. If the thread does not have an explicit uncaught exception handler set, and the thread's thread group (including parent thread groups) does not specialize its uncaughtException method, then the default handler's uncaughtException method will be invoked.

By setting the default uncaught exception handler, an application can change the way in which uncaught exceptions are handled (such as logging to a specific device, or file) for those threads that would already accept whatever "default" behavior the system provided.

If no other uncaught exception handler has been set, the one in ThreadGroup prints the exception's stack trace to System.err.

The specification does not seem to say which thread should execute the uncaught exception handler, but on the Oracle/OpenJDK JVM it is the thread that is about to terminate (that is, the one that threw the exception).

Comments

2

The exception will be handled by the JVM - it prints the stacktrace and exits. The same applies for Runtime-Exceptions that are not caught somewhere on their way up.

1 Comment

Thanks.So when main method throws exception(without handling using catch) which is thrown by a(as per my example),exception will be handled by JVM.Please correct me if i am wrong.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.