0
0
Spring Bootframework~10 mins

Handling not found exceptions in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Handling not found exceptions
Client sends request
Controller receives request
Service searches for resource
Resource found?
NoThrow NotFoundException
Exception Handler catches
Return resource data
Return 404 response
Send response to client
The client requests a resource; if the resource is missing, a NotFoundException is thrown and caught by a handler that returns a 404 response.
Execution Sample
Spring Boot
public ResponseEntity<User> getUser(Long id) {
  User user = userService.findById(id)
    .orElseThrow(() -> new UserNotFoundException(id));
  return ResponseEntity.ok(user);
}

@ExceptionHandler(UserNotFoundException.class)
public ResponseEntity<String> handleNotFound(UserNotFoundException ex) {
  return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage());
}
This code tries to find a user by ID; if not found, it throws an exception caught by a handler that returns a 404 error.
Execution Table
StepActionResource Found?Exception ThrownResponse Sent
1Client requests user with id=5N/ANoNo
2Controller calls service to find userNoNoNo
3Service returns empty OptionalNoNoNo
4orElseThrow triggers UserNotFoundExceptionNoYesNo
5ExceptionHandler catches UserNotFoundExceptionNoNoNo
6Handler returns 404 Not Found with messageNoNoYes
💡 Execution stops after sending 404 response because resource was not found.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
usernullnullnullException thrownException handled
Key Moments - 2 Insights
Why does the exception get thrown at step 4?
Because the service returned an empty Optional, orElseThrow triggers the UserNotFoundException as shown in execution_table row 4.
What happens after the exception is thrown?
The exception is caught by the ExceptionHandler method, which sends a 404 response, as shown in rows 5 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the UserNotFoundException thrown?
AStep 2
BStep 4
CStep 6
DStep 1
💡 Hint
Check the 'Exception Thrown' column in the execution_table at step 4.
According to the variable tracker, what is the value of 'user' after step 3?
Anull
BOptional.empty
CUser object
DException thrown
💡 Hint
Look at the 'user' row in variable_tracker after step 3.
If the user was found at step 3, what would change in the execution table?
AException would still be thrown at step 4
BExceptionHandler would catch an exception anyway
CNo exception thrown and 200 OK response sent
DResponse would be 404 Not Found
💡 Hint
Refer to the 'Resource Found?' and 'Response Sent' columns in execution_table.
Concept Snapshot
Handling Not Found Exceptions in Spring Boot:
- Use Optional.orElseThrow() to throw a custom exception if resource missing.
- Define @ExceptionHandler method to catch that exception.
- Return ResponseEntity with 404 status and message.
- This cleanly separates normal flow and error handling.
- Client receives clear 404 response when resource not found.
Full Transcript
In Spring Boot, when a client requests a resource, the controller calls the service to find it. If the resource is missing, the service returns an empty Optional. Using orElseThrow, the controller throws a custom NotFoundException. This exception is caught by an @ExceptionHandler method that returns a 404 Not Found response with a message. This flow ensures the client knows the resource was not found. The variable 'user' remains null since the exception is thrown before assignment. The exception handler sends the final response. This pattern cleanly handles missing data scenarios.