---
title: "how to catch multiple exceptions at once?"  
description: "how to catch multiple exceptions at once?"  
author: "Revati S Misra"  
published: 2023-07-20  
updated: 2023-07-20  
canonical: https://www.mindstick.com/forum/159178/how-to-catch-multiple-exceptions-at-once  
category: "exception handling"  
tags: ["exception handling", "exception"]  
reading_time: 2 minutes  

---

# how to catch multiple exceptions at once?

[Catch](https://www.mindstick.com/forum/159341/how-to-use-try-and-catch-in-java-for-exception-handling-and-what-happens-when-an-exception-occurs) [multiple exceptions](https://www.mindstick.com/forum/159240/how-to-handle-multiple-exceptions-using-the-catch-block-in-java) at once?

## Replies

### Reply by Aryan Kumar

Sure, here is how you can catch [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) [exceptions](https://www.mindstick.com/interview/22871/define-predifined-generic-exceptions) at once:

Java

```plaintext
try {
  // Code that might throw exceptions
} catch (ExceptionType1 | ExceptionType2 | ExceptionType3 e) {
  // Handle exceptions of type ExceptionType1, ExceptionType2, or ExceptionType3
}
```

For example, the following code will catch `ArithmeticException`, `NullPointerException`, and `ArrayIndexOutOfBoundsException` exceptions:

Java

```plaintext
try {
  int x = 10 / 0;
} catch (ArithmeticException | NullPointerException | ArrayIndexOutOfBoundsException e) {
  System.out.println("An exception occurred: " + e.getMessage());
}
```

When multiple exceptions are caught in a single catch block, the order of the exceptions is important. The first exception that is thrown will be the one that is handled by the catch block. If the catch block does not handle the exception, it will be rethrown.

It is important to note that if a catch block catches a more general exception type, it will also catch all of the more specific exception types. For example, the following code will catch both `ArithmeticException` and `NullPointerException` exceptions:

Java

```plaintext
try {
  int x = 10 / 0;
} catch (Exception e) {
  System.out.println("An exception occurred: " + e.getMessage());
}
```

This is because `Exception` is a more general exception type than `ArithmeticException` and `NullPointerException`.

Here are some additional things to keep in mind when catching multiple exceptions:

- The catch parameter is implicitly final, which means that you cannot assign any values to it.
- Catching multiple exceptions can reduce code duplication and improve the efficiency of your program.
- It is important to order the exceptions in the catch block correctly.
- Catching a more general exception type can also catch more specific exception types.


---

Original Source: https://www.mindstick.com/forum/159178/how-to-catch-multiple-exceptions-at-once

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
