Multiple Try-Catch Blocks in Java (With Examples and Explanation)

Multiple try catch blocks in Java example with code and explanation

Exception handling is a key part of writing stable Java programs. In real-world applications, a single piece of code can produce different types of errors. That’s where multiple try-catch blocks come into play.

In this guide, you’ll learn how to handle multiple exceptions efficiently using multiple catch blocks, along with simple examples that are easy to understand and perfect for exams.


What is Try-Catch in Java?

The try-catch block is used to handle exceptions in Java.

  • try → contains risky code
  • catch → handles the exception
Basic Try-Catch
try {
    int result = 10 / 0;
} catch (ArithmeticException e) {
    System.out.println("Cannot divide by zero");
}
  

What are Multiple Catch Blocks?

Multiple catch blocks allow you to handle different types of exceptions separately.

This makes your program more flexible and easier to debug.

Syntax
try {
    // risky code
} catch (ExceptionType1 e) {
    // handle exception
} catch (ExceptionType2 e) {
    // handle exception
}
  

Example Program (Multiple Catch Blocks)

Let’s see a program that handles multiple exceptions.

Multiple Catch Example
public class Test {
    public static void main(String[] args) {
        try {
            int a = 10;
            int b = 0;
            int arr[] = new int[5];

            int result = a / b; // ArithmeticException
            arr[10] = 50;       // ArrayIndexOutOfBoundsException

        } catch (ArithmeticException e) {
            System.out.println("Cannot divide by zero");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Array index is out of bounds");
        }

        System.out.println("Program continues...");
    }
}
  

Output

Program Output
Cannot divide by zero
Program continues...
  

Only the first exception is executed because once an exception occurs, the rest of the try block is skipped.


Nested Try-Catch Example

You can also use try-catch blocks inside another try block.

Nested Try Example
public class Test {
    public static void main(String[] args) {
        try {
            try {
                int a = 10 / 0;
            } catch (ArithmeticException e) {
                System.out.println("Handled inner exception");
            }

            int arr[] = new int[5];
            arr[10] = 20;

        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Handled outer exception");
        }
    }
}
  

Output

Output
Handled inner exception
Handled outer exception
  

Important Rules

  • Catch more specific exceptions first
  • General exception (Exception) should be last
  • Only one catch block executes at a time

Real-World Use Cases

  • Handling user input errors
  • File handling exceptions
  • Database operations
  • Network applications

Common Mistakes

  • Placing generic catch before specific one
  • Ignoring exception messages
  • Overusing nested try blocks

Best Practices

  • Handle exceptions properly
  • Use meaningful messages
  • Avoid catching generic Exception unless needed

FAQ

Can we use multiple catch blocks?

Yes, to handle different exceptions separately.

Which catch block executes?

The first matching catch block executes.

Can we use nested try?

Yes, but use it carefully.


Conclusion

Multiple try-catch blocks make your Java programs more robust and flexible. By handling different exceptions separately, you can create reliable and user-friendly applications.

Practice with different examples to master exception handling.


📚 Read More

Comments