0
0
Spring Bootframework~5 mins

Handling not found exceptions in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of handling 'not found' exceptions in Spring Boot?
To provide a clear and user-friendly response when a requested resource does not exist, avoiding server errors and improving user experience.
Click to reveal answer
beginner
Which annotation is commonly used in Spring Boot to handle exceptions globally?
The @ControllerAdvice annotation is used to define a class that handles exceptions across all controllers globally.
Click to reveal answer
intermediate
How do you return a 404 status code when a resource is not found in Spring Boot?
Throw a custom exception (e.g., ResourceNotFoundException) and handle it with @ExceptionHandler to return ResponseEntity with HttpStatus.NOT_FOUND (404).
Click to reveal answer
intermediate
What is the role of @ExceptionHandler in Spring Boot?
It marks a method that handles specific exceptions thrown in controller methods, allowing custom responses for errors like 'not found'.
Click to reveal answer
intermediate
Why is it better to use custom exceptions for 'not found' cases instead of returning null?
Custom exceptions clearly indicate an error state, making the code easier to read and maintain, and allowing centralized error handling with proper HTTP responses.
Click to reveal answer
Which HTTP status code should you return when a resource is not found?
A500
B404
C200
D302
Which Spring Boot annotation allows you to handle exceptions globally?
A@ControllerAdvice
B@RestController
C@Service
D@Repository
What does the @ExceptionHandler annotation do?
AHandles specific exceptions in controller methods
BDefines a REST endpoint
CInjects dependencies
DMarks a service class
What is a good practice when a resource is not found?
AReturn null silently
BReturn 200 with empty body
CThrow a custom exception and return 404
DThrow a generic exception
Where do you typically define exception handling methods in Spring Boot?
AIn the application.properties file
BInside the main application class
CIn the entity classes
DIn a class annotated with @ControllerAdvice
Explain how to handle a 'not found' exception in Spring Boot to return a 404 status code.
Think about how to signal missing data clearly to the user.
You got /4 concepts.
    Describe why using custom exceptions for 'not found' cases improves your Spring Boot application.
    Consider maintainability and user experience.
    You got /4 concepts.