Bird
0
0

Which of the following is the correct way to declare a custom exception class in Spring Boot?

easy📝 Syntax Q12 of 15
Spring Boot - Exception Handling
Which of the following is the correct way to declare a custom exception class in Spring Boot?
Apublic class MyException extends RuntimeException { public MyException(String message) { super(message); } }
Bpublic interface MyException extends RuntimeException {}
Cpublic class MyException implements RuntimeException {}
Dpublic class MyException extends Exception {}
Step-by-Step Solution
Solution:
  1. Step 1: Check correct inheritance

    Custom exceptions usually extend RuntimeException to avoid mandatory try-catch blocks.
  2. Step 2: Verify constructor usage

    Providing a constructor that calls super(message) is standard to pass error messages.
  3. Final Answer:

    public class MyException extends RuntimeException { public MyException(String message) { super(message); } } -> Option A
  4. Quick Check:

    Extend RuntimeException with constructor = D [OK]
Quick Trick: Extend RuntimeException and add constructor with message [OK]
Common Mistakes:
  • Using implements instead of extends
  • Extending Exception instead of RuntimeException
  • Declaring interface instead of class

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes