Bird
0
0

What will be the output of the following Java code?

medium📝 Predict Output Q13 of 15
Java - Exception Handling

What will be the output of the following Java code?

public class TestThrow {
    public static void main(String[] args) {
        try {
            throw new RuntimeException("Error happened");
        } catch (RuntimeException e) {
            System.out.println(e.getMessage());
        }
    }
}
AError happened
BRuntimeException
CCompilation error
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the try block

    The code throws a new RuntimeException with message "Error happened".
  2. Step 2: Analyze the catch block

    The catch block catches the exception and prints its message using e.getMessage(), which is "Error happened".
  3. Final Answer:

    Error happened -> Option A
  4. Quick Check:

    Exception message printed = C [OK]
Quick Trick: Catch prints exception message with getMessage() [OK]
Common Mistakes:
  • Expecting exception type name instead of message
  • Thinking code causes compilation error
  • Assuming no output without catch

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes