Bird
0
0

How can you globally handle a custom exception named OrderNotFoundException in a Spring Boot REST application to return a 404 status?

hard📝 Application Q9 of 15
Spring Boot - Exception Handling
How can you globally handle a custom exception named OrderNotFoundException in a Spring Boot REST application to return a 404 status?
ACreate a @ControllerAdvice class with a @ExceptionHandler method returning ResponseEntity with 404 status
BThrow the exception and rely on default 500 error handling
CCatch the exception in every controller method manually
DUse @RestController annotation on the exception class
Step-by-Step Solution
Solution:
  1. Step 1: Understand global exception handling

    @ControllerAdvice allows centralized exception handling across controllers.
  2. Step 2: Use @ExceptionHandler for custom exception

    Define a method annotated with @ExceptionHandler(OrderNotFoundException.class) that returns 404 status.
  3. Final Answer:

    Create a @ControllerAdvice class with a @ExceptionHandler method returning ResponseEntity with 404 status -> Option A
  4. Quick Check:

    Use @ControllerAdvice + @ExceptionHandler for global handling [OK]
Quick Trick: Use @ControllerAdvice and @ExceptionHandler for global exception handling [OK]
Common Mistakes:
  • Relying on default 500 error for custom exceptions
  • Catching exceptions manually everywhere
  • Misusing annotations on exception classes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes