Bird
Raised Fist0
Javaprogramming~20 mins

Multiple catch blocks in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
πŸŽ–οΈ
Multiple Catch Blocks Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of multiple catch blocks with specific exceptions
What is the output of the following Java code snippet?
Java
public class Test {
    public static void main(String[] args) {
        try {
            int[] arr = new int[2];
            System.out.println(arr[5]);
        } catch (ArithmeticException e) {
            System.out.println("ArithmeticException caught");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("ArrayIndexOutOfBoundsException caught");
        } catch (Exception e) {
            System.out.println("General Exception caught");
        }
    }
}
AArrayIndexOutOfBoundsException caught
BGeneral Exception caught
CArithmeticException caught
DCompilation error due to multiple catch blocks
Attempts:
2 left
πŸ’‘ Hint
Think about which exception is actually thrown and which catch block matches it first.
❓ Predict Output
intermediate
2:00remaining
Which catch block executes for NullPointerException?
Given the code below, which catch block will handle the exception?
Java
public class Test {
    public static void main(String[] args) {
        try {
            String s = null;
            System.out.println(s.length());
        } catch (IllegalArgumentException e) {
            System.out.println("IllegalArgumentException caught");
        } catch (NullPointerException e) {
            System.out.println("NullPointerException caught");
        } catch (Exception e) {
            System.out.println("General Exception caught");
        }
    }
}
AIllegalArgumentException caught
BNo exception caught, program runs normally
CNullPointerException caught
DGeneral Exception caught
Attempts:
2 left
πŸ’‘ Hint
What happens when you call a method on a null reference?
πŸ”§ Debug
advanced
2:00remaining
Identify the compilation error in multiple catch blocks
Why does the following code cause a compilation error?
Java
public class Test {
    public static void main(String[] args) {
        try {
            int a = 5 / 0;
        } catch (Exception e) {
            System.out.println("Exception caught");
        } catch (ArithmeticException e) {
            System.out.println("ArithmeticException caught");
        }
    }
}
ANo compilation error, code runs and prints "Exception caught"
BCompilation error because ArithmeticException catch block is unreachable
CCompilation error because try block has no code
DCompilation error because catch blocks must be in alphabetical order
Attempts:
2 left
πŸ’‘ Hint
Remember the order of catch blocks matters when exceptions have inheritance relationships.
❓ Predict Output
advanced
2:00remaining
Output when multiple exceptions are possible
What will be printed when running this code?
Java
public class Test {
    public static void main(String[] args) {
        try {
            int[] arr = null;
            System.out.println(arr.length);
            int a = 5 / 0;
        } catch (NullPointerException e) {
            System.out.println("NullPointerException caught");
        } catch (ArithmeticException e) {
            System.out.println("ArithmeticException caught");
        } catch (Exception e) {
            System.out.println("General Exception caught");
        }
    }
}
ANullPointerException caught
BArithmeticException caught
CGeneral Exception caught
DNo output, program crashes
Attempts:
2 left
πŸ’‘ Hint
Which exception happens first in the try block?
🧠 Conceptual
expert
2:00remaining
Why must catch blocks be ordered from specific to general?
In Java, why should catch blocks for exceptions be ordered from the most specific to the most general exception type?
ABecause catch blocks are executed in parallel and order does not matter
BBecause Java requires catch blocks to be alphabetically ordered by exception name
CBecause specific exceptions cannot be caught unless they are last
DBecause a general exception catch block before a specific one makes the specific catch block unreachable, causing a compilation error
Attempts:
2 left
πŸ’‘ Hint
Think about how Java matches exceptions to catch blocks and what happens if a parent exception is caught first.

Practice

(1/5)
1.

What is the main purpose of using multiple catch blocks in Java?

easy
A. To handle different types of exceptions separately
B. To run all catch blocks regardless of exception type
C. To improve program speed by skipping exceptions
D. To avoid using try blocks

Solution

  1. Step 1: Understand exception handling

    Multiple catch blocks allow handling different exceptions in different ways.
  2. Step 2: Identify the purpose

    Each catch block targets a specific exception type, so only the matching one runs.
  3. Final Answer:

    To handle different types of exceptions separately -> Option A
  4. Quick Check:

    Multiple catch blocks = handle exceptions separately [OK]
Hint: Multiple catch blocks handle different exceptions separately [OK]
Common Mistakes:
  • Thinking all catch blocks run for one exception
  • Believing catch blocks improve speed
  • Confusing catch blocks with try blocks
2.

Which of the following is the correct syntax for multiple catch blocks in Java?

try {
    // code
} catch (IOException e) {
    // handle IO
} catch (Exception e) {
    // handle general
}
easy
A. try { } catch (Exception e) { } catch (IOException e) { }
B. try { } catch (IOException e) { } catch (Exception e) { }
C. try { } catch IOException e { } catch Exception e { }
D. try { } catch (Exception e) catch (IOException e) { }

Solution

  1. Step 1: Check catch block order

    Specific exceptions like IOException must come before general ones like Exception.
  2. Step 2: Verify syntax correctness

    Each catch block must have parentheses around exception type and variable.
  3. Final Answer:

    try { } catch (IOException e) { } catch (Exception e) { } -> Option B
  4. Quick Check:

    Specific before general, correct syntax [OK]
Hint: Put specific exceptions before general ones in catch blocks [OK]
Common Mistakes:
  • Placing general exception before specific
  • Missing parentheses in catch
  • Combining catch blocks without braces
3.

What will be the output of the following code?

try {
    int[] arr = new int[2];
    System.out.println(arr[5]);
} catch (ArrayIndexOutOfBoundsException e) {
    System.out.println("Index error");
} catch (Exception e) {
    System.out.println("General error");
}
medium
A. No output
B. General error
C. ArrayIndexOutOfBoundsException
D. Index error

Solution

  1. Step 1: Identify exception thrown

    Accessing arr[5] causes ArrayIndexOutOfBoundsException.
  2. Step 2: Match catch block

    The first catch matches ArrayIndexOutOfBoundsException and prints "Index error".
  3. Final Answer:

    Index error -> Option D
  4. Quick Check:

    ArrayIndexOutOfBoundsException caught by first catch [OK]
Hint: First matching catch block runs for thrown exception [OK]
Common Mistakes:
  • Thinking general catch runs first
  • Expecting exception message printed
  • Assuming no output on exception
4.

Find the error in this code snippet:

try {
    int a = 5 / 0;
} catch (Exception e) {
    System.out.println("Error");
} catch (ArithmeticException e) {
    System.out.println("Math error");
}
medium
A. Try block missing braces
B. Exception catch block should be removed
C. ArithmeticException catch block should come before Exception catch block
D. No error, code is correct

Solution

  1. Step 1: Check catch block order

    More specific exceptions must come before general ones.
  2. Step 2: Identify error

    ArithmeticException is a subclass of Exception, so its catch must be first.
  3. Final Answer:

    ArithmeticException catch block should come before Exception catch block -> Option C
  4. Quick Check:

    Specific before general catch order [OK]
Hint: Place specific exceptions before general ones in catch blocks [OK]
Common Mistakes:
  • Putting general catch before specific
  • Ignoring catch block order rules
  • Assuming no compile error
5.

Consider this code:

try {
    String s = null;
    System.out.println(s.length());
} catch (NullPointerException e) {
    System.out.println("Null pointer caught");
} catch (RuntimeException e) {
    System.out.println("Runtime exception caught");
} catch (Exception e) {
    System.out.println("General exception caught");
}

What will be printed and why is the catch order important here?

hard
A. "Null pointer caught" because NullPointerException is caught first
B. "Runtime exception caught" because RuntimeException is more general
C. "General exception caught" because Exception is the base class
D. Compilation error due to catch order

Solution

  1. Step 1: Identify exception thrown

    Calling length() on null throws NullPointerException.
  2. Step 2: Check catch order

    NullPointerException is caught by the first catch block, which is specific and placed before general exceptions.
  3. Step 3: Understand importance of order

    If general exceptions came first, specific ones would be unreachable causing compile error.
  4. Final Answer:

    "Null pointer caught" because NullPointerException is caught first -> Option A
  5. Quick Check:

    Specific exceptions first, correct catch order [OK]
Hint: Catch specific exceptions before general ones to avoid errors [OK]
Common Mistakes:
  • Assuming general catch runs first
  • Ignoring NullPointerException specifics
  • Not knowing catch block order matters