Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
Java - Custom Exceptions
What will be the output of this code?
public class Test {
  static void method() throws Exception {
    throw new Exception("Error");
  }
  public static void main(String[] args) {
    try {
      method();
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }
}
ACompilation error
BException
CNo output
DError
Step-by-Step Solution
Solution:
  1. Step 1: Analyze exception throwing and catching

    The method throws an Exception with message "Error" which is caught in main's try-catch block.
  2. Step 2: Understand output from catch block

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

    Error -> Option D
  4. Quick Check:

    Exception message printed = "Error" [OK]
Quick Trick: Exception message prints when caught in try-catch [OK]
Common Mistakes:
  • Expecting 'Exception' instead of message
  • Thinking no output if exception thrown
  • Assuming compilation error due to throws

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes