Exception Propagation in Java

Naveen AnamaneniUpdated · 5 min read

When a method throws an exception and doesn't catch it, the exception doesn't just disappear. It leaves that method and appears in the caller, at the line that made the call. If the caller doesn't catch it either, it leaves the caller too, and so on, back up the chain of calls. This trip back through the callers is called propagation. Understanding it explains where to put your catch blocks and how to read a stack trace.

Watching It Travel

Each method without a matching catch is left; the first matching catch handles it
Propagate.java▶ Try it
public class Propagate {
    static int average(int[] marks) {
        int sum = 0;
        for (int m : marks) {
            sum += m;
        }
        System.out.println("  average: dividing " + sum
            + " by " + marks.length);
        return sum / marks.length;
    }
 
    static void printReport(String name, int[] marks) {
        System.out.println(" printReport: " + name);
        System.out.println(" " + name + " averages " + average(marks));
        System.out.println(" printReport finished");
    }
 
    public static void main(String[] args) {
        try {
            printReport("Navika", new int[] {80, 90});
            printReport("Srihitha", new int[] {});
            System.out.println("all reports done");
        } catch (ArithmeticException e) {
            System.out.println("main caught: " + e.getMessage());
        }
        System.out.println("main continues");
    }
}
Output
 printReport: Navika
  average: dividing 170 by 2
 Navika averages 85
 printReport finished
 printReport: Srihitha
  average: dividing 0 by 0
main caught: / by zero
main continues

How it works

  1. For Navika, every method finishes normally.
  2. For Srihitha, average divides by zero and throws ArithmeticException. It has no catch, so average ends right there.
  3. The exception appears in printReport, at the call to average. It has no catch either, so "Srihitha averages" and "printReport finished" never print.
  4. The exception appears in main, inside its try. The matching catch handles it, and "all reports done" is skipped too.
  5. main continues normally after the catch.

Checked Exceptions Need Permission

Unchecked exceptions, like the ArithmeticException above, propagate freely. A checked exception may only leave a method that declares it with throws (see "Java Throws Keyword"). So for checked exceptions, propagation is visible in every method signature along the way, which is exactly the point: callers are warned.

KindCan propagate out of a method...
Unchecked (RuntimeException, Error)Always, with no declaration
CheckedOnly if the method declares it with throws

Where Should the catch Go?

Propagation lets you choose the level at which to handle a problem. Usually that's the highest level that still knows what to do: a web request handler shows an error page, a batch job skips one record, a UI shows a message. Low-level methods like average usually shouldn't catch at all; they don't know what the right response is.

Common Mistakes

Catching too low. A method that catches and prints, then returns a fake value, hides the problem from callers who could have handled it properly.

Expecting code after the failing call to run. Every statement after the call in each method being left is skipped.

Forgetting throws for a checked exception you want to propagate. The code won't compile.

Interview Corner

What is exception propagation?

When an exception is thrown and not caught in the current method, the method terminates and the exception is passed to its caller at the point of the call. This repeats up the call stack until a matching catch block handles it. If it reaches the top of the thread's stack uncaught, the thread terminates and the default handler prints the stack trace. Unchecked exceptions propagate automatically; checked exceptions must be declared with throws to propagate.

Practice

  1. Easy: Add a try-catch inside printReport instead of main. What changes in the output, and does "all reports done" print now?
  2. Harder: Make average throw a checked exception of your own for an empty array. Which method signatures must change?

Quick check: Propagation

4 questions
1A method throws and has no matching catch. What happens?
2What happens to the caller's statements after the failing call?
3What must a method do to let a checked exception propagate?
4What happens if no method catches it?

Key Takeaways

Was this tutorial helpful?

Lesson 229 of 482