Middle
What happens when an exception is thrown in a thread?
sobes.tech AI
Answer from AI
Unwinding the call stack: the JVM searches for an appropriate exception handler (catch block) in the current method, then in the calling method, and so on up the stack.
If a handler is found:
- The code in the
catchblock is executed. - The
finallyblock may be executed. - Program execution continues after the
try-catch-finallyblock.
If no handler is found:
- The
uncaughtExceptionmethod of the correspondingThreadGroupis called, if it is overridden. - The default handler (
Thread.getDefaultUncaughtExceptionHandler) is used, if set. - If none of the above handlers are set, the exception's stack trace is printed to the standard error output, and the thread terminates.
In other threads, program execution continues independently of the thread that has finished.