---
title: "Handling multiple exceptions"  
description: "Handling multiple exceptions"  
author: "ICSM Computer"  
published: 2025-04-24  
updated: 2025-04-24  
canonical: https://www.mindstick.com/interview/34064/handling-multiple-exceptions  
category: "java"  
tags: ["java"]  
reading_time: 3 minutes  

---

# Handling multiple exceptions

Handling multiple exceptions in Java is common when a block of code might throw **more than one type of exception**. You can handle them using multiple `catch` blocks or a **multi-catch block**.

## 1. Multiple `catch` Blocks

You can write one `catch` block for each exception type.

## Example:

```java
public class MultipleCatchExample {
    public static void main(String[] args) {
        try {
            int[] arr = new int[3];
            arr[5] = 100; // ArrayIndexOutOfBoundsException

            int result = 10 / 0; // ArithmeticException
        }
        catch (ArithmeticException e) {
            System.out.println("ArithmeticException caught: " + e.getMessage());
        }
        catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("ArrayIndexOutOfBoundsException caught: " + e.getMessage());
        }
        catch (Exception e) {
            System.out.println("General exception caught: " + e.getMessage());
        }
    }
}
```

### Notes:

1. Order matters! Catch **more specific exceptions** before general ones (`Exception`).
2. If you reverse the order (put `Exception` first), the compiler will throw an error.

## 2. Multi-Catch Block (Java 7+)

You can catch multiple exceptions in a **single catch** using `|` (pipe symbol).

## Example:

```java
public class MultiCatchExample {
    public static void main(String[] args) {
        try {
            String s = null;
            System.out.println(s.length()); // NullPointerException

            int x = 10 / 0; // ArithmeticException
        }
        catch (NullPointerException | ArithmeticException e) {
            System.out.println("Caught an exception: " + e.getClass().getSimpleName());
        }
    }
}
```

### Multi-catch rules:

1. The exception types must not have a **parent-child relationship** (e.g., `IOException | Exception` is invalid).
2. Inside a multi-catch block, the exception variable (`e`) is **effectively final** – you can’t reassign it.

## Summary Table:

| Approach | Use When |
| --- | --- |
| Multiple `catch` blocks | Need different handling logic for each exception |
| Multi-catch (` | `) |

Let me know if you want to see this with custom exceptions too!

## Answers

### Answer by ICSM Computer

Handling multiple exceptions in Java is common when a block of code might throw **more than one type of exception**. You can handle them using multiple `catch` blocks or a **multi-catch block**.

## 1. Multiple `catch` Blocks

You can write one `catch` block for each exception type.

## Example:

```java
public class MultipleCatchExample {
    public static void main(String[] args) {
        try {
            int[] arr = new int[3];
            arr[5] = 100; // ArrayIndexOutOfBoundsException

            int result = 10 / 0; // ArithmeticException
        }
        catch (ArithmeticException e) {
            System.out.println("ArithmeticException caught: " + e.getMessage());
        }
        catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("ArrayIndexOutOfBoundsException caught: " + e.getMessage());
        }
        catch (Exception e) {
            System.out.println("General exception caught: " + e.getMessage());
        }
    }
}
```

### Notes:

1. Order matters! Catch **more specific exceptions** before general ones (`Exception`).
2. If you reverse the order (put `Exception` first), the compiler will throw an error.

## 2. Multi-Catch Block (Java 7+)

You can catch multiple exceptions in a **single catch** using `|` (pipe symbol).

## Example:

```java
public class MultiCatchExample {
    public static void main(String[] args) {
        try {
            String s = null;
            System.out.println(s.length()); // NullPointerException

            int x = 10 / 0; // ArithmeticException
        }
        catch (NullPointerException | ArithmeticException e) {
            System.out.println("Caught an exception: " + e.getClass().getSimpleName());
        }
    }
}
```

### Multi-catch rules:

1. The exception types must not have a **parent-child relationship** (e.g., `IOException | Exception` is invalid).
2. Inside a multi-catch block, the exception variable (`e`) is **effectively final** – you can’t reassign it.

## Summary Table:

| Approach | Use When |
| --- | --- |
| Multiple `catch` blocks | Need different handling logic for each exception |
| Multi-catch (` | `) |

Let me know if you want to see this with custom exceptions too!


---

Original Source: https://www.mindstick.com/interview/34064/handling-multiple-exceptions

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
