Java Programming Handbook

    Multiple and Nested Try-Catch Blocks in Java

    Introduction#

    In the previous blog, we covered the basics of try and catch blocks. Now, let's explore two advanced concepts:

    1. Multiple Try-Catch Blocks – Handling different exceptions separately.
    2. Nested Try-Catch Blocks – Handling exceptions within another try block.

    By the end of this blog, you’ll understand how to handle different exceptions efficiently and structure your error-handling logic effectively.

    Multiple Try-Catch Blocks#

    A try block can throw different types of exceptions. Java allows multiple catch blocks to handle different exceptions separately.

    Syntax:#

    try { // Risky code } catch (ExceptionType1 e1) { // Handle ExceptionType1 } catch (ExceptionType2 e2) { // Handle ExceptionType2 } catch (Exception e) { // Handle any other exceptions }

    Example:#

    public class MultipleTryCatchExample { public static void main(String[] args) { try { int[] numbers = {1, 2, 3}; int result = 10 / 0; // ArithmeticException System.out.println(numbers[5]); // ArrayIndexOutOfBoundsException (unreachable in this case) } catch (ArithmeticException e) { System.out.println("Error: Cannot divide by zero."); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Error: Array index is out of bounds."); } catch (Exception e) { System.out.println("A general exception occurred: " + e.getMessage()); } } }

    Output:#

    Error: Cannot divide by zero.

    👉 Notice that the second exception (ArrayIndexOutOfBoundsException) never occurs because the first error stops execution.

    Best Practice: Always catch specific exceptions first, then use a generic Exception catch block at the end.

    Nested Try-Catch Blocks#

    A try block inside another try block is called a nested try-catch. This is useful when handling exceptions at different levels in a program.

    Why Use Nested Try-Catch?#

    • When you have operations that can fail independently.
    • To handle exceptions at different scopes in a program.
    • To prevent a single error from stopping the entire program.

    Syntax:#

    try { try { // Inner risky code } catch (ExceptionType1 e) { // Handle specific exception } } catch (ExceptionType2 e) { // Handle outer exception }

    Example:#

    public class NestedTryCatchExample { public static void main(String[] args) { try { System.out.println("Outer try block"); try { int[] numbers = {1, 2, 3}; System.out.println(numbers[5]); // ArrayIndexOutOfBoundsException } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Inner catch: Array index out of bounds."); } int result = 10 / 0; // ArithmeticException (outer block) } catch (ArithmeticException e) { System.out.println("Outer catch: Cannot divide by zero."); } System.out.println("Program continues..."); } }

    Output:#

    Outer try block Inner catch: Array index out of bounds. Outer catch: Cannot divide by zero. Program continues...

    👉 The inner exception (ArrayIndexOutOfBoundsException) is handled separately from the outer one (ArithmeticException).

    Key Differences Between Multiple and Nested Try-Catch#

    FeatureMultiple Try-CatchNested Try-Catch
    StructureMultiple catch blocks after a single try blockOne try-catch inside another try block
    Handling ScopeHandles different exceptions independentlyHandles exceptions at different levels
    Use CaseWhen multiple errors may occur in a single blockWhen errors occur at different levels of execution

    Best Practices for Nested and Multiple Try-Catch#

    • Use multiple catch blocks when different exceptions need separate handling.
    • Use nested try-catch when different parts of the code require independent exception handling.
    • Avoid deep nesting – too many nested try blocks make code harder to read.
    • Always close resources properly (use finally or try-with-resources).

    Conclusion#

    In this blog, we explored:

    • Multiple try-catch blocks for handling different exceptions separately.
    • Nested try-catch blocks for handling errors at different levels.
    • Best practices to write clean and efficient exception handling code.

    Next, we will dive into Class Exception in Java, explaining how exception classes work and how to create custom exceptions.

    Last updated on Apr 09, 2025