CodeBlanks Logo Icon CodeBlanks

Multiple catch

Learn multiple catch blocks.

Multiple catch blocks

A single try can be followed by multiple catch blocks. Each catches a different exception type. Order matters: specific types come first, general types later.

try {
  int n = Integer.parseInt(s);
  int r = 10 / n;
} catch (NumberFormatException e) {
  System.out.println("Sayı değil");
} catch (ArithmeticException e) {
  System.out.println("Sıfıra bölme");
}

If you put a general type first, the specific ones become unreachable and you get a compile error. So you must place general types like Exception last. Each catch is independent; only one runs.

Multi-catch

Since Java 7 you can catch multiple types in a single catch. Types are separated by |; this is called multi-catch.

try {
  int n = Integer.parseInt(s);
} catch (NumberFormatException | ArithmeticException e) {
  System.out.println("Hata");
}

Multi-catch avoids code duplication for different types that need the same handling. However, the types separated by | cannot be related by inheritance; they must be siblings. The e variable is implicitly final; it cannot be modified inside.

Practice Catch two types with separate catch

Define String s = "abc";. Inside try write Integer.parseInt(s) and int r = 10 / n;. The first catch catches NumberFormatException and prints Sayı değil; the second catches ArithmeticException and prints Sıfıra bölme.

Starter code

Main.java
public class Main {
  public static void main(String[] args) {
    String s = "abc";
    // attempt parseInt and division, catch with two separate catch blocks
  }
}

Sample solution

Main.java
try {
  int n = Integer.parseInt(s);
  int r = 10 / n;
  System.out.println(r);
} catch (NumberFormatException e) {
  System.out.println("Sayı değil");
} catch (ArithmeticException e) {
  System.out.println("Sıfıra bölme");
}

Expected output

Sayı değil

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Add the second catch

Question
try {
  int n = Integer.parseInt(s);
} catch (NumberFormatException e) {
  System.out.println("Sayı değil");
} ____ (ArithmeticException e) {
  System.out.println("Sıfıra bölme");
}
Answer
try {
  int n = Integer.parseInt(s);
} catch (NumberFormatException e) {
  System.out.println("Sayı değil");
} catch (ArithmeticException e) {
  System.out.println("Sıfıra bölme");
}

Explanation: the second catch catches a different specific type.

2 How should multiple catch blocks be ordered?

  • General first, then specific
  • Specific first, then general Correct answer
  • Only one catch is allowed
  • Order does not matter

Explanation: specific types come first; general types go last, otherwise a compile error occurs.

3 Match each scenario to its catch type

Term Meaning
parseInt fails NumberFormatException
division by zero ArithmeticException
null object NullPointerException
out of bounds ArrayIndexOutOfBoundsException

All lessons in Beginner