0
0
Spring Bootframework~10 mins

Handling not found exceptions in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a custom exception class named ResourceNotFoundException.

Spring Boot
public class ResourceNotFoundException extends [1] {
    public ResourceNotFoundException(String message) {
        super(message);
    }
}
Drag options to blanks, or click blank then click option'
AThrowable
BException
CError
DRuntimeException
Attempts:
3 left
💡 Hint
Common Mistakes
Extending Exception instead of RuntimeException causes the exception to be checked.
Extending Error is not appropriate for application exceptions.
2fill in blank
medium

Complete the code to annotate the exception handler method to catch ResourceNotFoundException.

Spring Boot
@[1](ResourceNotFoundException.class)
public ResponseEntity<String> handleNotFound(ResourceNotFoundException ex) {
    return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage());
}
Drag options to blanks, or click blank then click option'
ARequestMapping
BExceptionHandler
CResponseStatus
DControllerAdvice
Attempts:
3 left
💡 Hint
Common Mistakes
Using @RequestMapping instead of @ExceptionHandler.
Confusing @ControllerAdvice with @ExceptionHandler.
3fill in blank
hard

Fix the error in the exception handler method to return NOT_FOUND status correctly.

Spring Boot
return ResponseEntity.status([1]).body(ex.getMessage());
Drag options to blanks, or click blank then click option'
AHttpStatus.OK
BHttpStatus.INTERNAL_SERVER_ERROR
CHttpStatus.NOT_FOUND
DHttpStatus.BAD_REQUEST
Attempts:
3 left
💡 Hint
Common Mistakes
Returning HttpStatus.OK (200) which means success.
Using INTERNAL_SERVER_ERROR (500) which is for server errors.
4fill in blank
hard

Fill both blanks to create a global exception handler class with proper annotation and method signature.

Spring Boot
@[1]
public class GlobalExceptionHandler {

    @ExceptionHandler(ResourceNotFoundException.class)
    public ResponseEntity<String> [2](ResourceNotFoundException ex) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage());
    }
}
Drag options to blanks, or click blank then click option'
AControllerAdvice
BhandleResourceNotFound
CRestController
DhandleException
Attempts:
3 left
💡 Hint
Common Mistakes
Using @RestController instead of @ControllerAdvice for global exception handling.
Using generic method names like handleException without clarity.
5fill in blank
hard

Fill all three blanks to throw the custom exception when a resource is not found in the service method.

Spring Boot
public Resource getResourceById(Long id) {
    return repository.findById(id).orElseThrow(() -> new [1]([2]));
}

// Message constant
private static final String [3] = "Resource not found with id: ";
Drag options to blanks, or click blank then click option'
AResourceNotFoundException
B"Resource not found with id: " + id
CNOT_FOUND_MESSAGE
DIllegalArgumentException
Attempts:
3 left
💡 Hint
Common Mistakes
Throwing IllegalArgumentException instead of the custom exception.
Using a message variable name that is not defined.