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
✗ Incorrect
404 means 'Not Found', which is the correct status when a requested resource does not exist.
Which Spring Boot annotation allows you to handle exceptions globally?
A@ControllerAdvice
B@RestController
C@Service
D@Repository
✗ Incorrect
@ControllerAdvice lets you define global exception handling logic.
What does the @ExceptionHandler annotation do?
AHandles specific exceptions in controller methods
BDefines a REST endpoint
CInjects dependencies
DMarks a service class
✗ Incorrect
@ExceptionHandler marks methods that handle exceptions thrown in controllers.
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
✗ Incorrect
Throwing a custom exception and returning 404 clearly communicates the error.
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
✗ Incorrect
Exception handling methods are usually placed in a @ControllerAdvice class for global handling.
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.
Practice
(1/5)
1. What is the main purpose of handling not found exceptions in a Spring Boot application?
easy
A. To improve database performance
B. To log all user activities
C. To automatically retry failed requests
D. To return a clear 404 status when a requested resource is missing
Solution
Step 1: Understand the role of not found exceptions
Not found exceptions indicate that a requested resource does not exist on the server.
Step 2: Identify the correct response behavior
Returning a 404 HTTP status code clearly informs the client that the resource is missing.
Final Answer:
To return a clear 404 status when a requested resource is missing -> Option D
Quick Check:
Not found exceptions = 404 status [OK]
Hint: Not found means resource missing, so send 404 status [OK]
Common Mistakes:
Confusing not found with server errors
Thinking it improves database speed
Assuming it retries requests automatically
2. Which annotation is used in Spring Boot to mark a custom exception class to return HTTP 404 status automatically?
easy
A. @ResponseStatus(HttpStatus.NOT_FOUND)
B. @ExceptionHandler(HttpStatus.NOT_FOUND)
C. @NotFoundException
D. @HttpStatusCode(404)
Solution
Step 1: Recall Spring Boot annotations for HTTP status
The @ResponseStatus annotation sets the HTTP status code for exceptions or controller methods.
Step 2: Match the annotation to 404 status
Using @ResponseStatus(HttpStatus.NOT_FOUND) on a custom exception class sends 404 automatically.
Final Answer:
@ResponseStatus(HttpStatus.NOT_FOUND) -> Option A
Quick Check:
@ResponseStatus + NOT_FOUND = 404 [OK]
Hint: Use @ResponseStatus with NOT_FOUND for 404 exceptions [OK]
Common Mistakes:
Using @ExceptionHandler instead of @ResponseStatus
Assuming @NotFoundException is a valid annotation
Using incorrect annotation names
3. Given this controller method in Spring Boot:
@GetMapping("/items/{id}")
public Item getItem(@PathVariable Long id) {
return itemRepository.findById(id)
.orElseThrow(() -> new ItemNotFoundException(id));
}
What happens if the item with the given id does not exist?
medium
A. The method returns null and HTTP 200 status
B. An ItemNotFoundException is thrown and HTTP 404 status is returned
C. The server crashes with a NullPointerException
D. The method returns an empty Item object
Solution
Step 1: Analyze the use of orElseThrow
If the item is not found, orElseThrow triggers the custom ItemNotFoundException.
Step 2: Understand exception handling with @ResponseStatus
If ItemNotFoundException is annotated with @ResponseStatus(HttpStatus.NOT_FOUND), Spring returns 404 status.
Final Answer:
An ItemNotFoundException is thrown and HTTP 404 status is returned -> Option B
@ResponseStatus(HttpStatus.NOT_FOUND)
public class ProductNotFoundException extends RuntimeException {
public ProductNotFoundException(Long id) {
super("Product " + id + " not found");
}
}
@GetMapping("/products/{id}")
public Product getProduct(@PathVariable Long id) {
return productRepo.findById(id).orElseThrow(() -> new ProductNotFoundException(id));
}
What is the likely problem if the client always receives HTTP 500 instead of 404 when a product is missing?
medium
A. The exception is thrown but Spring does not map it to 404
B. The exception is not annotated with @ResponseStatus
C. The controller method does not handle the exception explicitly
D. The exception class is missing a no-argument constructor
Solution
Step 1: Verify annotation presence
The exception class is correctly annotated with @ResponseStatus(HttpStatus.NOT_FOUND).
Step 2: Understand Spring's exception mapping
If Spring returns 500, it means it does not recognize the exception mapping to 404, possibly due to package scanning or configuration issues.
Final Answer:
The exception is thrown but Spring does not map it to 404 -> Option A
Quick Check:
Annotated exception but 500 means mapping failed [OK]
Hint: 500 instead of 404 means Spring missed exception mapping [OK]
Common Mistakes:
Assuming missing no-arg constructor causes 500
Thinking @ResponseStatus is missing when it is present
Believing explicit handler is always required
5. You want to create a global exception handler in Spring Boot that returns a JSON error with 404 status and a message when any ResourceNotFoundException occurs. Which approach correctly achieves this?
hard
A. Use @RestController on the exception class and return 404 status
B. Annotate ResourceNotFoundException with @ResponseStatus(HttpStatus.NOT_FOUND) only, no handler needed
C. Create a class annotated with @ControllerAdvice and a method with @ExceptionHandler(ResourceNotFoundException.class) returning ResponseEntity with status 404 and error body
D. Throw ResponseStatusException with 404 inside every controller method manually
Solution
Step 1: Understand global exception handling
Using @ControllerAdvice with @ExceptionHandler allows centralized handling of exceptions across controllers.
Step 2: Return ResponseEntity with 404 and JSON body
The handler method can build a ResponseEntity with HttpStatus.NOT_FOUND and a JSON error message for clients.
Final Answer:
Create a class annotated with @ControllerAdvice and a method with @ExceptionHandler(ResourceNotFoundException.class) returning ResponseEntity with status 404 and error body -> Option C
Quick Check:
Global handler + ResponseEntity = custom 404 JSON [OK]
Hint: Use @ControllerAdvice + @ExceptionHandler for global JSON 404 [OK]
Common Mistakes:
Relying only on @ResponseStatus without custom JSON body
Annotating exception class as controller
Manually throwing exceptions in every method
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.