0
0
Spring Bootframework~5 mins

Custom exception classes in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a custom exception class in Spring Boot?
A custom exception class is a user-defined class that extends Java's Exception or RuntimeException to represent specific error conditions in a Spring Boot application.
Click to reveal answer
beginner
Why create custom exception classes instead of using standard exceptions?
Custom exceptions help clearly identify and handle specific error cases, making the code easier to read, maintain, and debug.
Click to reveal answer
beginner
How do you define a simple custom exception class in Spring Boot?
Create a class that extends RuntimeException and add constructors. For example:<br><pre>public class ResourceNotFoundException extends RuntimeException {<br>  public ResourceNotFoundException(String message) {<br>    super(message);<br>  }<br>}</pre>
Click to reveal answer
intermediate
How can custom exceptions improve REST API error responses in Spring Boot?
By using @ControllerAdvice and @ExceptionHandler, you can catch custom exceptions and return meaningful HTTP status codes and messages to clients.
Click to reveal answer
intermediate
What is the difference between checked and unchecked exceptions in the context of custom exceptions?
Checked exceptions extend Exception and must be declared or caught. Unchecked exceptions extend RuntimeException and do not require explicit handling. Custom exceptions are usually unchecked for cleaner code.
Click to reveal answer
Which class should you extend to create an unchecked custom exception in Spring Boot?
AException
BRuntimeException
CThrowable
DError
What annotation is commonly used to globally handle custom exceptions in Spring Boot?
A@ControllerAdvice
B@RestController
C@Service
D@Component
Which method is used inside a custom exception class to pass an error message?
AtoString()
BgetMessage()
CprintStackTrace()
Dsuper(message)
Why might you prefer unchecked exceptions for custom exceptions in Spring Boot?
AThey force callers to handle exceptions explicitly.
BThey are slower to execute.
CThey simplify code by not requiring try-catch blocks.
DThey cannot be caught.
What HTTP status code is typically returned when a ResourceNotFoundException is thrown in a REST API?
A404 Not Found
B400 Bad Request
C500 Internal Server Error
D200 OK
Explain how to create and use a custom exception class in a Spring Boot REST API.
Think about how exceptions improve error clarity and API responses.
You got /5 concepts.
    Describe the benefits of using custom exceptions over standard exceptions in Spring Boot applications.
    Consider how custom exceptions help both developers and users.
    You got /5 concepts.