0
0
Javaprogramming~20 mins

Multiple catch blocks in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.