0
0
Spring Bootframework~10 mins

@ExceptionHandler in controllers 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 handle exceptions in a Spring Boot controller method.

Spring Boot
public class MyController {
    @[1]
    public ResponseEntity<String> handleException(Exception ex) {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error occurred");
    }
}
Drag options to blanks, or click blank then click option'
ARequestMapping
BPostMapping
CGetMapping
DExceptionHandler
Attempts:
3 left
💡 Hint
Common Mistakes
Using @GetMapping or @PostMapping instead of @ExceptionHandler
Forgetting to annotate the method
2fill in blank
medium

Complete the code to specify which exception type the handler method should catch.

Spring Boot
@ExceptionHandler([1].class)
public ResponseEntity<String> handleNotFound(Exception ex) {
    return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Resource not found");
}
Drag options to blanks, or click blank then click option'
AResourceNotFoundException
BRuntimeException
CNullPointerException
DIOException
Attempts:
3 left
💡 Hint
Common Mistakes
Using a general exception like RuntimeException instead of a specific one
Not specifying the exception class
3fill in blank
hard

Fix the error in the exception handler method signature to correctly handle exceptions.

Spring Boot
@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<String> handleException([1] ex) {
    return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage());
}
Drag options to blanks, or click blank then click option'
AString
BException
CResourceNotFoundException
DHttpServletRequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using a generic Exception type when a specific exception is declared
Using unrelated types like String or HttpServletRequest
4fill in blank
hard

Fill both blanks to create a global exception handler class with proper annotations.

Spring Boot
@[1]
public class GlobalExceptionHandler {
    @[2]
    public ResponseEntity<String> handleAllExceptions(Exception ex) {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Server error");
    }
}
Drag options to blanks, or click blank then click option'
AControllerAdvice
BRestController
CExceptionHandler
DService
Attempts:
3 left
💡 Hint
Common Mistakes
Using @RestController instead of @ControllerAdvice for global handling
Missing @ExceptionHandler on the method
5fill in blank
hard

Fill all three blanks to handle a specific exception and return a custom message with status.

Spring Boot
@ExceptionHandler([1].class)
public ResponseEntity<String> handleCustomException([2] ex) {
    return ResponseEntity.status([3]).body("Custom error: " + ex.getMessage());
}
Drag options to blanks, or click blank then click option'
ACustomException
CHttpStatus.BAD_REQUEST
DHttpStatus.NOT_FOUND
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching exception class and parameter type
Using wrong HTTP status codes