Bird
0
0

Which of the following is the correct way to declare an exception handler method in a Spring Boot controller?

easy📝 Syntax Q12 of 15
Spring Boot - Exception Handling
Which of the following is the correct way to declare an exception handler method in a Spring Boot controller?
A@RequestMapping(Exception.class) public String handleError() { return "error"; }
B@GetMapping(Exception.class) public String handleError() { return "error"; }
C@Autowired(Exception.class) public String handleError() { return "error"; }
D@ExceptionHandler(Exception.class) public String handleError() { return "error"; }
Step-by-Step Solution
Solution:
  1. Step 1: Identify the correct annotation for exception handling

    @ExceptionHandler is the annotation used to mark methods that handle exceptions.
  2. Step 2: Check the method signature

    The method should be annotated with @ExceptionHandler(Exception.class) and return a response, here a String view name.
  3. Final Answer:

    @ExceptionHandler(Exception.class) public String handleError() { return "error"; } -> Option D
  4. Quick Check:

    @ExceptionHandler syntax [OK]
Quick Trick: Use @ExceptionHandler with exception class to handle errors [OK]
Common Mistakes:
  • Using @RequestMapping or @GetMapping instead of @ExceptionHandler
  • Misusing @Autowired annotation
  • Missing the exception class in @ExceptionHandler

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes