---
title: "Exception Handling in Java: Combining Exception Handlers"  
description: "In my previous post, we have learnt how we can handle multiple exceptions in our code by providing multiple numbers of catch block specific to a parti"  
author: "Ailsa Singh"  
published: 2016-05-20  
updated: 2018-03-15  
canonical: https://www.mindstick.com/blog/11111/exception-handling-in-java-combining-exception-handlers  
category: "java"  
tags: ["exception handling", "java"]  
reading_time: 3 minutes  

---

# Exception Handling in Java: Combining Exception Handlers

In my previous post, we have learnt how we can handle [multiple exceptions](https://www.mindstick.com/forum/159240/how-to-handle-multiple-exceptions-using-the-catch-block-in-java) in our code by providing multiple numbers of [catch block](https://www.mindstick.com/forum/159348/how-does-an-exception-find-the-correct-catch-block) specific to a particular exception. But Java SE 7 introduces a very handy feature by which we can **combine several exceptions** in a single catch block. Let’s learn this new feature.

**Look at the following code segment that contains multiple exception handlers:**

```
try {
// Say some file parser code here...
} catch (IOException ex) {
// log and rethrow exception
} catch (ParseException ex) {
// log and rethrow exception
} catch (ClassNotFoundException ex) {
// log and rethrow exception
}
```

The code parses the contents of a given file. The process may generate different types of exceptions, such as IOException, ParseException, and ClassNotFoundException. Each exception handler for these errors logs the exception and re-throws another exception to the caller. We discuss this re-throwing [business](https://www.mindstick.com/articles/12213/how-to-start-a-jewellery-making-business-online-business-ideas) in later posts. What is important here is that all exception handlers execute the same piece of code. So why not combine them? Java SE 7 facilitates this.

##### Here’s what the new code looks like:

```
try {
// Say some file parser code here...
     } catch (IOException ex | ParseException ex |
              ClassNotFoundException ex) {
                     // log and rethrow exception
   }
```

##### \

Here, all those exception handlers that have common code are combined by using a **logical OR operator** between their exception types. This makes the code simpler. However, this feature works only on Java SE 7 and above. If we want to take different actions for different exceptions, we do not have a choice other than to provide individual exception handlers, as stated earlier.

##### How Runtime Matches catch Blocks

Whenever an [exception occurs](https://www.mindstick.com/forum/159341/how-to-use-try-and-catch-in-java-for-exception-handling-and-what-happens-when-an-exception-occurs) in running code, the search for the first matching catch block begins and the rules listed here are followed:

•A thrown exception object is caught by the catch block that specifies the class of the occurred exception or its superclass.

•In the case of [multiple catch](https://www.mindstick.com/forum/159236/explain-the-concept-of-multiple-catch-blocks-in-java-and-their-order-of-execution) blocks, these are evaluated sequentially in the order they are specified by applying the first rule. If a catch block is found, the rest of the catch blocks are ignored.

•A certain catch block will never be executed if a catch block containing its superclass is listed prior to it. In such situations, a compile-time error is generated.

•The compiler forces the programmer to handle all checked exceptions. In other words, we must provide [error handling](https://www.mindstick.com/forum/160168/error-handling-in-go) for all exceptions except for the [RuntimeException](https://www.mindstick.com/forum/159354/runtimeexception-and-exception-behavior) and its subclasses.

•If the try block never throws an exception specified in the catch list, the compiler generates an error.

---

Original Source: https://www.mindstick.com/blog/11111/exception-handling-in-java-combining-exception-handlers

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
