0
0
Spring Bootframework~5 mins

@ExceptionHandler in controllers in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the @ExceptionHandler annotation in Spring Boot controllers?
It is used to define a method that handles specific exceptions thrown within the controller, allowing custom error responses without crashing the app.
Click to reveal answer
beginner
How do you specify which exception a method handles using @ExceptionHandler?
You pass the exception class as a parameter to <code>@ExceptionHandler</code>, for example: <code>@ExceptionHandler(NullPointerException.class)</code>.
Click to reveal answer
intermediate
Can @ExceptionHandler methods return custom HTTP status codes?
Yes, by returning a ResponseEntity or using @ResponseStatus on the method, you can set custom HTTP status codes for error responses.
Click to reveal answer
intermediate
Where can @ExceptionHandler methods be declared in a Spring Boot app?
They can be declared inside a controller class or in a separate class annotated with <code>@ControllerAdvice</code> to handle exceptions globally.
Click to reveal answer
beginner
What happens if no @ExceptionHandler matches an exception thrown in a controller?
Spring Boot uses its default error handling, which usually returns a generic error page or JSON with status 500 Internal Server Error.
Click to reveal answer
What does @ExceptionHandler do in a Spring Boot controller?
AConfigures database connections
BDefines a new REST endpoint
CInjects dependencies into the controller
DHandles specific exceptions thrown in the controller
How do you specify which exception a method handles with @ExceptionHandler?
ABy passing the exception class to the annotation
BBy naming the method after the exception
CBy returning a ResponseEntity
DBy using <code>@ResponseBody</code>
Where can @ExceptionHandler methods be placed?
AOnly inside controller classes
BInside controllers or classes annotated with <code>@ControllerAdvice</code>
COnly inside service classes
DOnly in configuration classes
What is the default HTTP status code if an exception is not handled by @ExceptionHandler?
A500 Internal Server Error
B404 Not Found
C200 OK
D302 Redirect
How can you customize the HTTP status code returned by an @ExceptionHandler method?
ABy naming the method with the status code
BBy using <code>@GetMapping</code>
CBy returning a <code>ResponseEntity</code> with status
DBy throwing another exception
Explain how @ExceptionHandler works in Spring Boot controllers and how it helps improve user experience.
Think about how apps show friendly error messages instead of crashing.
You got /5 concepts.
    Describe where and how you would use @ExceptionHandler to handle a NullPointerException in a Spring Boot app.
    Imagine you want to catch a specific error and show a nice message.
    You got /4 concepts.