Spring Boot Exception Handling

    Question 1CORE SPRING BOOT - Purpose of @ControllerAdvice

    What is the main purpose of the @ControllerAdvice annotation in Spring Boot?

    Question 2CORE SPRING BOOT - Purpose of @ExceptionHandler

    Which statement correctly describes @ExceptionHandler in Spring Boot?

    Question 3CORE SPRING BOOT - Code Snippet (Global Exception Handler)

    What will happen when a NullPointerException is thrown in any controller in this code?

    @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(NullPointerException.class) public ResponseEntity<String> handleNull(Exception ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Null value found!"); } }

    Question 4CORE SPRING BOOT - Local vs Global Exception Handling

    Which statement is correct regarding local vs global exception handling in Spring Boot?

    Question 5CORE SPRING BOOT - Code Snippet (Multiple Exceptions)

    What does the following code achieve?

    @ControllerAdvice public class GlobalHandler { @ExceptionHandler({IOException.class, SQLException.class}) public ResponseEntity<String> handleIOAndSQL(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body("Error: " + ex.getMessage()); } }

    Question 6CORE SPRING BOOT - ResponseEntity in Exception Handling

    Why is ResponseEntity commonly used in exception handlers?

    Question 7CORE SPRING BOOT - Code Snippet (@RestControllerAdvice)

    What is the effect of using @RestControllerAdvice instead of @ControllerAdvice?

    @RestControllerAdvice public class GlobalRestHandler { @ExceptionHandler(IllegalArgumentException.class) public Map<String, String> handleIllegalArgs(IllegalArgumentException ex) { Map<String, String> map = new HashMap<>(); map.put("error", ex.getMessage()); return map; } }

    Question 8CORE SPRING BOOT - Code Snippet (Default Exception Handling)

    What will happen if an unhandled exception occurs in a Spring Boot controller and no @ControllerAdvice is defined?

    @RestController public class SampleController { @GetMapping("/test") public String test() { throw new RuntimeException("Something went wrong!"); } }

    Question 9CORE SPRING BOOT - Best Practices in Exception Handling

    Which of the following is a best practice for exception handling in Spring Boot?

    Question 10CORE SPRING BOOT - Custom Error Attributes

    How can you customize the structure of default error responses in Spring Boot?