💥 Exceptions & Errors · Intermediate

try / catch in Java

Catching exceptions, catch order from specific to general.

🧩 The mysteryAn exception is like a fire alarm: everyone stops what they're doing and heads for the nearest exit that fits. Which statements still run — and which are skipped?

The alarm rings

Wrap risky code in **try. When something throws, Java skips the rest of the try block and jumps to the first catch whose type matches. After the catch, execution continues after the whole try statement**.

try {
    int n = Integer.parseInt(text);
    use(n);                 // skipped on error
} catch (NumberFormatException e) {
    System.out.println("not a number");
}
🔮 Predict it

Follow the alarm

What does this print?

try {
    System.out.print("1");
    String s = null;
    s.length();
    System.out.print("2");
} catch (NullPointerException e) {
    System.out.print("3");
}
System.out.println("4");
  1. 134
  2. 1234
  3. 13
  4. 124
Show the answer

1 prints, s.length() throws, **2 is skipped**, the catch prints 3, then execution continues after the try with 4.

First match wins

Catches are checked top to bottom, and a catch matches its type and every subclass. The first match handles it; the rest are ignored. So put specific types before general ones: FileNotFoundException before IOException.

try {
    open(file);
} catch (FileNotFoundException e) {
    // specific first
} catch (IOException e) {
    // then the general case
}
🔮 Predict it

Which catch?

5 / 0 throws ArithmeticException. What prints?

try {
    int x = 5 / 0;
} catch (IllegalArgumentException e) {
    System.out.println("arg");
} catch (RuntimeException e) {
    System.out.println("runtime");
}
  1. arg
  2. runtime
  3. arg runtime
Show the answer

ArithmeticException isn't an IllegalArgumentException, but it is a RuntimeException, so the second catch handles it. Only one catch ever runs.

⚠️ The trap

General first = unreachable

Put catch (Exception e) first and it swallows every NumberFormatException before the next catch could see it. That later catch can never run, and Java treats it as a compile error.

try {
    Integer.parseInt("x");
} catch (Exception e) {
} catch (NumberFormatException e) { // error
}
🤔 Think first

Nobody answers the alarm

An ArrayIndexOutOfBoundsException is thrown, but the only catch is for ArithmeticException. What happens to the code after the try?

Think about it, then reveal the answer

It never runs. No catch matches, so the exception propagates out of the method to its caller, and so on. If it escapes main, the program dies with a stack trace.

💼 In the real world

Catching narrowly

Parsing user input is the classic use: catch (NumberFormatException e) shows a friendly "please enter a number" message. Catching just that type means real bugs — say, a NullPointerException in your own code — still surface instead of being mislabelled as bad input.

Key takeaways

  1. Statements after the throw in try are skipped
  2. The first matching catch wins; the rest are ignored
  3. Specific before general: FileNotFoundException before IOException
  4. Afterwards, execution continues after the whole try statement

💡 Catch blocks are safety nets stacked from fine mesh to wide mesh — the first one that fits catches the fall.

🤯 Did you know?

A try block costs essentially nothing when nothing is thrown: the compiler records the protected range in a method's exception table instead of adding instructions to the normal path.

Practice questions

What does this print?

try {
    System.out.print("A");
    int x = 1 / 0;
    System.out.print("B");
} catch (ArithmeticException e) {
    System.out.print("C");
}
System.out.println("D");
  1. ACD
  2. ABCD
  3. AC
  4. ABD
Check your answer

ACD. A prints, 1 / 0 throws, B is skipped, the catch prints C, then execution continues with D.

What does this print?

try {
    Integer.parseInt("x");
} catch (Exception e) {
    System.out.println("general");
} catch (NumberFormatException e) {
    System.out.println("number");
}
  1. general
  2. number
  3. Compile error
  4. general number
Check your answer

Compile error. catch (Exception e) already handles every NumberFormatException, so the second catch is unreachable — a compile error.

Next: two catch blocks doing the exact same thing? A single | merges them into one.