Bird
0
0

How do you define a method inside a @ControllerAdvice class to handle IllegalStateException globally?

easy📝 Syntax Q3 of 15
Spring Boot - Exception Handling
How do you define a method inside a @ControllerAdvice class to handle IllegalStateException globally?
AAnnotate the method with <code>@ExceptionHandler(IllegalStateException.class)</code> and return a suitable response
BUse <code>@RequestMapping</code> annotation on the method with the exception class as a parameter
CDeclare the method as <code>public void handle()</code> without any annotations
DAnnotate the method with <code>@Controller</code> inside the <code>@ControllerAdvice</code> class
Step-by-Step Solution
Solution:
  1. Step 1: Use @ExceptionHandler

    To handle exceptions globally in a @ControllerAdvice class, the method must be annotated with @ExceptionHandler specifying the exception type.
  2. Step 2: Specify Exception Class

    Pass the exception class, e.g., IllegalStateException.class, as the value to @ExceptionHandler to target that exception.
  3. Step 3: Return Appropriate Response

    The method should return a response entity or view name to handle the exception properly.
  4. Final Answer:

    Annotate the method with @ExceptionHandler(IllegalStateException.class) and return a suitable response -> Option A
  5. Quick Check:

    Method must have @ExceptionHandler with exception class [OK]
Quick Trick: Use @ExceptionHandler with exception class inside @ControllerAdvice [OK]
Common Mistakes:
  • Using @RequestMapping instead of @ExceptionHandler
  • Not annotating the method at all
  • Using @Controller annotation inside @ControllerAdvice

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes