So basically, if we use the throws
keyword in a method signature, we are passing the handling responsibility to the caller method; if we use the throw
keyword, we are explicitly throwing an exception. Both throws
and throw
are not ways to handle an exception. Only the statements in the catch
block can be considered the way to handle an exception since our program won't terminate since we have handled the exception. Am I getting it correctly? Since in Java interviews we are always asked how do we handle exceptions in Java.
4 Answers
This is correct.
When an exception is thrown, you have only two options - either catch
ing or allowing it to be thrown upwards to the caller. Ultimately, if it's never caught, the thread will terminate.

- 297,002
- 52
- 306
- 350
-
Isn't it also possible for a `try/finally` block to gobble an exception? – supercat Aug 28 '21 at 19:49
-
A `finally` block won't prevent the exception from being thrown - it will still need to be caught **somewhere**. – Mureinik Aug 28 '21 at 19:54
-
@user16320675 yes, indeed. Good catch. Edited and fixed. – Mureinik Aug 28 '21 at 20:38
-
What if a `return` or `throw` is executed within the finally block? My recollection is that in the former case, Java will forget that an exception occurred, and in the latter case the new exception will erase any "recollection" of the previous one. – supercat Aug 28 '21 at 21:24
Yeah I would agree mostly agree.
- You can catch the exception, and then the exception and handle that (possibly with use of a finally block as well so maybe mention that).
- Throw upwards again.
- If you're doing threading you can also set the uncaught exception behavior, so you could "handle" the exception that way in a sense, but that is a specific situation and not always applicable.

- 11
- 1
Exceptions are handled with try-catch
block which wraps the code throwing an exception. No matter how it's issued with throw
clause or indirectly via calling a method which has throws
in the method signature. If the method throws an exception and does not declare it in the method signature then this is kind of unchecked exceptions. These exceptions are handled the same way as checked exceptions that you have to catch or declare to be thrown.
See more about exceptions in Java Tutorial.

- 49,761
- 33
- 66
- 176
Shortly, answer ordered like that:
- try-catch
- Thread::setUncaughtExceptionHandler
- ThreadGroup::uncaughtException(Thread, Throwable)
- [Globally] Thread.setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler)
Please note, that this list is more about JVM mechanisms rather then 'frameworks'.
If you want more I believe you can play with code below and see docs of used methods and classes.
public static void main(String[] args) throws InterruptedException {
System.out.println("START main");
Thread.setDefaultUncaughtExceptionHandler((t, e) -> {
System.err.println("Thread.setDefaultUncaughtExceptionHandler");
});
Thread.currentThread().setUncaughtExceptionHandler((t, e) -> {
System.err.println("thread[main].uncaughtException");
});
java.util.concurrent.ThreadFactory threadFactory = r -> {
ThreadGroup g = new ThreadGroup("eta") {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.err.println("threadGroup.uncaughtException");
}
};
Thread thread = new Thread(g, r);
thread.setUncaughtExceptionHandler((t, e) -> {
System.err.println("thread.uncaughtException");
});
return thread;
// return new Thread(r);
};
threadFactory.newThread(() -> {
System.out.println("START newThread logic");
throw new RuntimeException();
}).start();
if (true) throw new RuntimeException();
}
Ouputs:
START main
START newThread logic
thread[main].uncaughtException
thread.uncaughtException

- 358
- 2
- 10