Bird
0
0

Which of the following is the correct way to declare a custom checked exception in Java?

easy📝 Syntax Q12 of 15
Java - Custom Exceptions
Which of the following is the correct way to declare a custom checked exception in Java?
Aclass MyException extends RuntimeException {}
Bclass MyException extends Error {}
Cclass MyException extends Exception {}
Dclass MyException extends Throwable {}
Step-by-Step Solution
Solution:
  1. Step 1: Identify checked exceptions

    Checked exceptions in Java must extend Exception but not RuntimeException.
  2. Step 2: Analyze each option

    Extending RuntimeException creates an unchecked exception. Extending Error is for system errors. Extending Exception creates a checked exception. Extending Throwable is too general.
  3. Final Answer:

    class MyException extends Exception {} -> Option C
  4. Quick Check:

    Checked exceptions extend Exception [OK]
Quick Trick: Checked exceptions extend Exception class [OK]
Common Mistakes:
  • Confusing checked and unchecked exceptions
  • Extending Error instead of Exception
  • Extending Throwable directly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes