Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q4 of 15
Java - Exception Handling

What will be the output of the following code?

public class TestThrow {
    public static void main(String[] args) {
        try {
            throw new ArithmeticException("Error");
        } catch (ArithmeticException e) {
            System.out.println("Caught: " + e.getMessage());
        }
    }
}
AError
BCaught: ArithmeticException
CCaught: Error
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the try block

    The code throws an ArithmeticException with message "Error".
  2. Step 2: Analyze the catch block

    The catch block catches the exception and prints "Caught: " plus the exception message.
  3. Final Answer:

    Caught: Error -> Option C
  4. Quick Check:

    throw + catch prints message = Caught: Error [OK]
Quick Trick: Catch prints 'Caught: ' plus exception message [OK]
Common Mistakes:
  • Expecting just 'Error' without 'Caught: '
  • Thinking it causes compilation error
  • Confusing exception class name with message
  • Missing catch block

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes