Bird
0
0

How do you correctly annotate a method to handle NullPointerException in a Spring Boot controller?

easy📝 Syntax Q3 of 15
Spring Boot - Exception Handling
How do you correctly annotate a method to handle NullPointerException in a Spring Boot controller?
A@HandleException(NullPointerException.class)
B@ExceptionHandler(NullPointerException.class)
C@ExceptionHandler("NullPointerException")
D@Exception(NullPointerException.class)
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct annotation

    The correct annotation is @ExceptionHandler with the exception class as parameter.
  2. Step 2: Check syntax

    The parameter must be the class type, not a string or other annotation.
  3. Final Answer:

    @ExceptionHandler(NullPointerException.class) -> Option B
  4. Quick Check:

    Use @ExceptionHandler with exception class [OK]
Quick Trick: Use @ExceptionHandler with exception class type [OK]
Common Mistakes:
  • Using incorrect annotation names
  • Passing exception name as string
  • Omitting .class suffix

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes