0
0
Spring Bootframework~10 mins

Why centralized error handling matters in Spring Boot - Test Your Understanding

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

Complete the code to define a global exception handler class in Spring Boot.

Spring Boot
@[1]
public class GlobalExceptionHandler {
}
Drag options to blanks, or click blank then click option'
ARestController
BControllerAdvice
CService
DComponent
Attempts:
3 left
💡 Hint
Common Mistakes
Using @RestController instead of @ControllerAdvice
Forgetting the annotation entirely
2fill in blank
medium

Complete the method annotation to handle exceptions of type RuntimeException.

Spring Boot
@ExceptionHandler([1].class)
public ResponseEntity<String> handleRuntimeException(RuntimeException ex) {
    return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ex.getMessage());
}
Drag options to blanks, or click blank then click option'
AThrowable
BException
CError
DRuntimeException
Attempts:
3 left
💡 Hint
Common Mistakes
Using @ExceptionHandler(Exception.class) which is too broad
Using @ExceptionHandler(Error.class) which is for serious errors
3fill in blank
hard

Fix the error in the method signature to correctly return a ResponseEntity with a message.

Spring Boot
public [1] handleNotFoundException(EntityNotFoundException ex) {
    return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Entity not found");
}
Drag options to blanks, or click blank then click option'
AResponseEntity<String>
BString
Cvoid
DHttpStatus
Attempts:
3 left
💡 Hint
Common Mistakes
Returning void causes no response body
Returning String only sends body without status code
4fill in blank
hard

Fill both blanks to create a method that handles IllegalArgumentException and returns a BAD_REQUEST status.

Spring Boot
@ExceptionHandler([1].class)
public [2] handleIllegalArgument(IllegalArgumentException ex) {
    return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
}
Drag options to blanks, or click blank then click option'
AIllegalArgumentException
BResponseEntity<String>
Cvoid
DString
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong exception type in annotation
Returning void or String instead of ResponseEntity
5fill in blank
hard

Fill all three blanks to create a global handler method for NullPointerException with INTERNAL_SERVER_ERROR status and a custom message.

Spring Boot
@ExceptionHandler([1].class)
public [2] handleNullPointer(NullPointerException ex) {
    return ResponseEntity.status(HttpStatus.[3]).body("Null value encountered");
}
Drag options to blanks, or click blank then click option'
ANullPointerException
BResponseEntity<String>
CINTERNAL_SERVER_ERROR
DBAD_REQUEST
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong exception type
Wrong return type
Wrong HTTP status code