0
0
Javaprogramming~5 mins

Multiple catch blocks in Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of multiple catch blocks in Java?
Multiple catch blocks allow a program to handle different types of exceptions separately, providing specific responses for each exception type.
Click to reveal answer
beginner
How does Java decide which catch block to execute when an exception occurs?
Java checks the exception type thrown and matches it with the first compatible catch block in order. It executes the first matching catch block and skips the rest.
Click to reveal answer
beginner
Can you have multiple catch blocks for a single try block in Java?
Yes, you can have multiple catch blocks after a single try block to handle different exception types separately.
Click to reveal answer
intermediate
What happens if a more general exception catch block is placed before a more specific one?
The compiler will give an error because the more general catch block will catch all exceptions of that type and its subclasses, making the specific catch block unreachable.
Click to reveal answer
beginner
Write a simple Java try-catch example with multiple catch blocks for ArithmeticException and NullPointerException.
try { int result = 10 / 0; String text = null; System.out.println(text.length()); } catch (ArithmeticException e) { System.out.println("Cannot divide by zero."); } catch (NullPointerException e) { System.out.println("Null value encountered."); }
Click to reveal answer
What will happen if an exception is thrown that does not match any catch block?
AThe program will terminate with an uncaught exception error.
BThe first catch block will handle it anyway.
CThe program will ignore the exception and continue.
DJava will automatically create a catch block.
Which catch block will execute if both ArithmeticException and Exception catch blocks are present and an ArithmeticException is thrown?
AThe catch block for Exception.
BThe catch block for ArithmeticException.
CBoth catch blocks execute.
DNone will execute.
Is it valid to have multiple catch blocks for the same exception type?
ANo, it causes a compile-time error.
BNo, Java merges them automatically.
CYes, but only if they are in different try blocks.
DYes, to handle the same exception differently.
What is the correct order of catch blocks?
AOnly one catch block is allowed.
BFrom most general to most specific exception.
COrder does not matter.
DFrom most specific to most general exception.
Can a try block have no catch blocks?
AYes, but only in Java 17+.
BNo, catch blocks are mandatory.
CYes, if it has a finally block.
DNo, try blocks must always have catch blocks.
Explain how multiple catch blocks work in Java and why their order matters.
Think about how Java matches exceptions to catch blocks.
You got /3 concepts.
    Write a Java try-catch example with at least two catch blocks for different exceptions and explain what each block does.
    Use ArithmeticException and NullPointerException for clarity.
    You got /3 concepts.