0
0
Spring Bootframework~20 mins

Handling not found exceptions in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exception Mastery in Spring Boot
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a resource is not found in this Spring Boot controller?
Consider this Spring Boot REST controller method that fetches a user by ID. What will be the HTTP response status if the user is not found?
Spring Boot
@GetMapping("/users/{id}")
public ResponseEntity<User> getUser(@PathVariable Long id) {
    Optional<User> user = userService.findById(id);
    if (user.isPresent()) {
        return ResponseEntity.ok(user.get());
    } else {
        throw new UserNotFoundException("User not found");
    }
}
AHTTP 404 Not Found with a custom error message
BHTTP 500 Internal Server Error
CHTTP 200 OK with an empty body
DHTTP 400 Bad Request
Attempts:
2 left
💡 Hint
Think about what happens when an exception is thrown and how Spring Boot handles it by default or with custom handlers.
📝 Syntax
intermediate
2:00remaining
Which code snippet correctly defines a custom exception handler for not found exceptions in Spring Boot?
You want to return HTTP 404 when a UserNotFoundException is thrown. Which of these @ExceptionHandler methods is correct?
A
@ExceptionHandler(UserNotFoundException.class)
@ResponseBody
public ResponseEntity&lt;String&gt; handleUserNotFound() {
    return new ResponseEntity&lt;&gt;("User not found", HttpStatus.NOT_FOUND);
}
B
@ExceptionHandler
@ResponseStatus(HttpStatus.NOT_FOUND)
public void handleUserNotFound(UserNotFoundException ex) {
    return "User not found";
}
C
@ExceptionHandler(UserNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handleUserNotFound() {
    return "User not found";
}
D
@ExceptionHandler(UserNotFoundException.class)
public ResponseEntity&lt;String&gt; handleUserNotFound() {
    return new ResponseEntity&lt;&gt;("User not found", HttpStatus.NOT_FOUND);
}
Attempts:
2 left
💡 Hint
Remember that the method signature and return type must match the expected response type.
🔧 Debug
advanced
2:00remaining
Why does this global exception handler not return 404 for UserNotFoundException?
Given this global exception handler, why does the client receive HTTP 500 instead of 404 when UserNotFoundException is thrown?
Spring Boot
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public String handleAllExceptions(Exception ex) {
        return "Error occurred";
    }
}
ABecause the handler catches all exceptions as 500, it overrides specific handlers and does not map UserNotFoundException to 404
BBecause UserNotFoundException is not a subclass of Exception, so it is not caught
CBecause @ResponseStatus is ignored in @ControllerAdvice classes
DBecause the method returns a String instead of ResponseEntity
Attempts:
2 left
💡 Hint
Think about exception handler specificity and how Spring chooses which handler to use.
state_output
advanced
2:00remaining
What is the HTTP response body when this exception handler is triggered?
Given this exception handler, what will the client receive in the response body when UserNotFoundException is thrown?
Spring Boot
@ExceptionHandler(UserNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
@ResponseBody
public Map<String, String> handleUserNotFound(UserNotFoundException ex) {
    Map<String, String> error = new HashMap<>();
    error.put("error", ex.getMessage());
    return error;
}
A"User not found"
B{"error":"User not found"}
C{}
D<error>User not found</error>
Attempts:
2 left
💡 Hint
Consider the return type and how Spring converts it to JSON.
🧠 Conceptual
expert
3:00remaining
Why use @ResponseStatus on a custom exception class instead of handling it in a controller advice?
What is the main advantage of annotating a custom exception class with @ResponseStatus(HttpStatus.NOT_FOUND) compared to handling the exception in a @ControllerAdvice class?
AIt forces Spring Boot to log the exception stack trace every time it occurs
BIt disables the default error page and returns a blank response instead
CIt allows the exception to automatically trigger the specified HTTP status without extra handler code, simplifying controller logic
DIt makes the exception checked, so it must be declared or caught
Attempts:
2 left
💡 Hint
Think about how Spring Boot uses @ResponseStatus on exceptions during response generation.