Bird
0
0

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

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

    Checked exceptions must extend Exception but not RuntimeException. Unchecked exceptions extend RuntimeException.
  2. Step 2: Identify the correct superclass for a checked custom exception

    Extending Exception creates a checked exception, which requires handling or declaration.
  3. Final Answer:

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

    Checked exception = extends Exception [OK]
Quick Trick: Checked exceptions extend Exception, unchecked extend RuntimeException [OK]
Common Mistakes:
  • Extending RuntimeException for checked exceptions
  • Extending Error which is for serious JVM errors
  • Extending Throwable directly without reason

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes