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?✗ Incorrect
@ExceptionHandler is used to catch and handle exceptions in controller methods.How do you specify which exception a method handles with
@ExceptionHandler?✗ Incorrect
You specify the exception class inside
@ExceptionHandler(ExceptionClass.class).Where can
@ExceptionHandler methods be placed?✗ Incorrect
@ExceptionHandler methods can be in controllers or global handler classes with @ControllerAdvice.What is the default HTTP status code if an exception is not handled by
@ExceptionHandler?✗ Incorrect
Unhandled exceptions usually result in a 500 Internal Server Error response.
How can you customize the HTTP status code returned by an
@ExceptionHandler method?✗ Incorrect
Returning a
ResponseEntity lets you set the HTTP status code explicitly.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.