What happens when a Java method throws an exception that it does not catch?
answer
- Innermost frame first, climb to main
- Unwind = pop frames one at a time
- First matching catch wins
- finally runs while unwinding
- No match = uncaught handler prints trace, thread dies
basics
~20 sIf a method does not catch an exception, Java stops running that method and hands the exception to the method that called it. This repeats up the call chain until some method catches it, or the program prints the error and stops.
solid answer
~40 sWhen code throws an exception and the current method has no matching catch, normal execution of that method halts immediately and the exception is passed to the caller. The JVM searches each enclosing method on the call stack, from innermost to outermost, for a try block whose catch can handle that exception type. The first matching catch handles it and execution resumes after that try/catch. If no method on the stack matches, the exception reaches the top of the thread. The thread's default uncaught-exception handler then prints the exception and its stack trace to standard error and that thread terminates. In a single-threaded program that ends the program; other threads are unaffected.
go deeper
Knows that an uncaught exception is passed to the caller and eventually prints a stack trace and stops the program.
Explains stack unwinding frame by frame, the innermost-first search for a matching catch, and that finally runs during propagation.
Distinguishes per-thread termination from JVM exit, references the UncaughtExceptionHandler, and reads the trace's frame order correctly.
Discusses cost of stack unwinding, designing exception boundaries, and configuring thread-level uncaught handlers for resilience in production systems.
## The setup: what is the call stack? When a Java program runs, each method call is recorded on a structure called the **call stack**. A **stack frame** is created for every method invocation; it holds that method's local variables and remembers where to return when the method finishes. If `main` calls `a`, and `a` calls `b`, and `b` calls `c`, the stack (innermost on top) looks like: `c` → `b` → `a` → `main`. The method currently running is always the top frame. ## What an exception is An **exception** is an object (a subclass of `java.lang.Throwable`) that represents an abnormal event — e.g. dividing by zero (`ArithmeticException`) or calling a method on a `null` reference (`NullPointerException`). You can also create one and **throw** it explicitly with the `throw` keyword. Throwing means: stop normal execution right here and signal a problem. ## Propagation: unwinding the stack When an exception is thrown in method `c`, the JVM first checks whether the throwing statement sits inside a `try` block in `c` that has a `catch` clause matching the exception's type (the exception type, or any supertype of it). - **If yes**, that `catch` handles it; `c` continues normally after the try/catch. - **If no**, `c` is abandoned: its frame is removed from the stack (this removal is called **unwinding**), and the exception moves to the caller, `b`. The same check runs in `b`, then `a`, then `main`. This climb from innermost frame outward is **exception propagation**. The key rule: propagation goes **up the call stack, one frame at a time, innermost first**, and stops at the **first** frame with a matching handler. ## What if nobody catches it? If the exception propagates past `main` (or past the top method of any thread) with no match, it is **uncaught**. The JVM hands it to the thread's `UncaughtExceptionHandler`. The default handler prints a message like: ``` Exception in thread "main" java.lang.ArithmeticException: / by zero at C.c(Example.java:12) at B.b(Example.java:8) at A.a(Example.java:4) at Example.main(Example.java:1) ``` The **stack trace** lists the frames in the order they were unwound — top is where the exception was thrown, bottom is `main`. That thread then terminates. In a single-threaded app this ends the JVM. ## finally still runs If a frame being unwound has a `finally` block (a block guaranteed to run when leaving a `try`), that `finally` executes **during** propagation, before the exception continues upward. This is how resources get cleaned up even on the error path. ## Deriving the answer From these pieces: throw → check current frame → no match → unwind frame (run its finally) → repeat in caller → first match handles it → else uncaught handler prints the trace and the thread dies.
- Does an uncaught exception in a worker thread stop the whole JVM?No. It only terminates that thread. The JVM keeps running as long as other non-daemon threads are alive. Only an uncaught exception on the last running non-daemon thread (commonly main) ends the program.
- In what order does the stack trace print the frames?Top to bottom from where the exception was thrown down to the outermost caller. The first 'at' line is the throw site; the last is typically main or the thread's run method.
saying these in an interview costs you the question
- Saying the exception goes down to the called methods — it goes UP to callers.
- Thinking an uncaught exception always crashes the entire program (only true on the last non-daemon thread).
- Claiming finally is skipped when an exception propagates — it runs during unwinding.
- Believing multiple catch blocks all run — only the first matching one does.