---
title: "Multiple catch block in java"  
description: "Hi everyone in this blog I’m explaining about the multi-catch block of the single try block.  Introduction:If you have to perform different tasks at t"  
author: "Anonymous User"  
published: 2015-05-23  
updated: 2018-02-26  
canonical: https://www.mindstick.com/blog/10896/multiple-catch-block-in-java  
category: "exception handling"  
tags: ["java", "exception"]  
reading_time: 2 minutes  

---

# Multiple catch block in java

Hi everyone in this blog I’m explaining about the multi-[catch block](https://www.mindstick.com/forum/159348/how-does-an-exception-find-the-correct-catch-block) of the single try block.\

#### Introduction:

##

If you have to perform different tasks at the occurrence of different [Exceptions](https://www.mindstick.com/interview/22871/define-predifined-generic-exceptions), use java multi-catch block.

Let's see a [simple](https://yourviews.mindstick.com/story/1469/5-simple-ways-to-stay-fit-amp-healthy) example of java multi-catch block.

```
static void main(string … args){    try    {        int a[] = new int[5];        a[5] = 30/0 ;        System.out.println(a[5]);    }    catch(ArithmeticException ex)    {        System.out.println ("task1 is completed...");    }    catch (IndexOutOfBoundException ex)    {        System.out.println ("task2 is completed...");    }    catch (Exception ex)    {        System.out.println ("common task is completed...");    }    System.out.println("rest of the code...");}
```

**[Output](https://www.mindstick.com/forum/161319/how-does-the-print-function-handle-output-in-python):**

##

Task1 completed…

Rest of the code…

\

**Rule 1:** At a time only one [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) is occurred and at a time only one catch block is executed.\

**Rule 2:** All catch blocks must be [ordered](https://www.mindstick.com/forum/156534/difference-between-ordered-and-unordered-list-in-html) from most specific to most general i.e. catch for [ArithmeticException](https://www.mindstick.com/forum/159243/how-to-handle-the-arithmeticexception-in-java) must come before catch for Exception .

```
static void main(string … args){    try    {        int[] a = new int[5];        a[5] = 30/0 ;        System.out.println(a[4]);    }    catch (Exception ex)    {        System.out.println("common task is completed...");    }    catch(ArithmeticException ex)    {        System.out.println("task1 is completed...");    }    catch (IndexOutOfBoundException ex)    {        System.out.println("task2 is completed...");    }    System.out.println("rest of the code...");}
```

##

## Output:

[Compile](https://www.mindstick.com/forum/2316/how-to-views-compile-in-mvc) time [Error](https://yourviews.mindstick.com/view/88527/fixing-quickbooks-error-4120-reinstalling-vs-repairing)

---

Original Source: https://www.mindstick.com/blog/10896/multiple-catch-block-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
