Sobes.tech
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 catch block is executed.
  • The finally block may be executed.
  • Program execution continues after the try-catch-finally block.

If no handler is found:

  • The uncaughtException method of the corresponding ThreadGroup is 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.

What happens when an exception is thrown in a thread… - sobes.tech