0
0
Spring Bootframework~20 mins

Custom exception classes in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spring Boot Exception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a custom exception is thrown in a Spring Boot REST controller?

Consider a Spring Boot REST controller that throws a custom exception ResourceNotFoundException when a resource is missing. What will the client receive if no global exception handler is defined?

Spring Boot
public class ResourceNotFoundException extends RuntimeException {
    public ResourceNotFoundException(String message) {
        super(message);
    }
}

@RestController
public class MyController {
    @GetMapping("/item/{id}")
    public String getItem(@PathVariable String id) {
        throw new ResourceNotFoundException("Item not found");
    }
}
AThe client receives a 500 Internal Server Error with a default error page.
BThe client receives a 404 Not Found status with the exception message in the response body.
CThe client receives a 400 Bad Request status with a JSON error message.
DThe client receives a 200 OK status with an empty response body.
Attempts:
2 left
💡 Hint

Think about what Spring Boot does by default when an exception is thrown and no handler is present.

📝 Syntax
intermediate
2:00remaining
Which custom exception class definition is syntactically correct in Spring Boot?

Choose the correct way to define a custom unchecked exception class named InvalidInputException that accepts a message.

A
public class InvalidInputException extends RuntimeException {
    public InvalidInputException(String message) {
        super(message);
    }
}
B
public class InvalidInputException extends Exception {
    public InvalidInputException(String message) {
        super(message);
    }
}
C
public class InvalidInputException {
    public InvalidInputException(String message) {
        super(message);
    }
}
D
public class InvalidInputException extends Throwable {
    public InvalidInputException(String message) {
        super(message);
    }
}
Attempts:
2 left
💡 Hint

Remember that unchecked exceptions extend RuntimeException.

🔧 Debug
advanced
2:00remaining
Why does this custom exception handler not catch the exception?

Given the following code, why does the handleCustomException method not handle MyCustomException?

Spring Boot
public class MyCustomException extends RuntimeException {
    public MyCustomException(String message) {
        super(message);
    }
}

@RestController
public class DemoController {
    @GetMapping("/test")
    public String test() {
        throw new MyCustomException("Error occurred");
    }
}

@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(IllegalArgumentException.class)
    public ResponseEntity<String> handleCustomException(IllegalArgumentException ex) {
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
    }
}
ABecause @ControllerAdvice is missing @RestController annotation.
BBecause the handler listens for IllegalArgumentException, not MyCustomException.
CBecause MyCustomException must extend Exception, not RuntimeException.
DBecause the handler method must return void to catch exceptions.
Attempts:
2 left
💡 Hint

Check the exception type specified in the @ExceptionHandler annotation.

state_output
advanced
2:00remaining
What is the HTTP status code returned by this custom exception handler?

Given this custom exception and handler, what HTTP status code will the client receive when DataConflictException is thrown?

Spring Boot
public class DataConflictException extends RuntimeException {
    public DataConflictException(String message) {
        super(message);
    }
}

@ControllerAdvice
public class ApiExceptionHandler {
    @ExceptionHandler(DataConflictException.class)
    public ResponseEntity<String> handleConflict(DataConflictException ex) {
        return ResponseEntity.status(HttpStatus.CONFLICT).body(ex.getMessage());
    }
}
A200 OK
B404 Not Found
C500 Internal Server Error
D409 Conflict
Attempts:
2 left
💡 Hint

Look at the status code used in the ResponseEntity.

🧠 Conceptual
expert
2:00remaining
Why use custom exception classes in Spring Boot applications?

Which of the following is the best reason to create custom exception classes in a Spring Boot app?

ATo avoid using try-catch blocks anywhere in the code.
BTo reduce the size of the application by replacing all exceptions with one class.
CTo provide specific error types that can be handled separately and return meaningful HTTP responses.
DTo automatically log all errors without additional configuration.
Attempts:
2 left
💡 Hint

Think about how custom exceptions improve error handling and client communication.