Bird
Raised Fist0
Spring Bootframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of creating a custom exception class in Spring Boot?
easy
A. To automatically fix errors without any code changes
B. To replace all built-in exceptions with your own versions
C. To make the application run faster by skipping error checks
D. To handle specific error cases clearly and separately in your application

Solution

  1. Step 1: Understand the role of custom exceptions

    Custom exceptions help you identify and manage specific error situations clearly in your code.
  2. Step 2: Compare with other options

    Replacing all exceptions or auto-fixing errors is not the goal; custom exceptions improve clarity and control.
  3. Final Answer:

    To handle specific error cases clearly and separately in your application -> Option D
  4. Quick Check:

    Custom exceptions clarify error handling = C [OK]
Hint: Custom exceptions clarify specific errors clearly [OK]
Common Mistakes:
  • Thinking custom exceptions replace all errors
  • Believing they fix errors automatically
  • Assuming they improve performance directly
2. Which of the following is the correct way to declare a custom exception class in Spring Boot?
easy
A. public class MyException extends RuntimeException { public MyException(String message) { super(message); } }
B. public interface MyException extends RuntimeException {}
C. public class MyException implements RuntimeException {}
D. public class MyException extends Exception {}

Solution

  1. Step 1: Check correct inheritance

    Custom exceptions usually extend RuntimeException to avoid mandatory try-catch blocks.
  2. Step 2: Verify constructor usage

    Providing a constructor that calls super(message) is standard to pass error messages.
  3. Final Answer:

    public class MyException extends RuntimeException { public MyException(String message) { super(message); } } -> Option A
  4. Quick Check:

    Extend RuntimeException with constructor = D [OK]
Hint: Extend RuntimeException and add constructor with message [OK]
Common Mistakes:
  • Using implements instead of extends
  • Extending Exception instead of RuntimeException
  • Declaring interface instead of class
3. Given this custom exception class:
public class DataNotFoundException extends RuntimeException {
  public DataNotFoundException(String message) {
    super(message);
  }
}

What will happen if this exception is thrown in a Spring Boot controller without any handler?
medium
A. The exception will be ignored and the request will succeed
B. The application will return a 500 Internal Server Error response
C. The application will automatically return a 404 Not Found response
D. The server will crash and stop running

Solution

  1. Step 1: Understand default Spring Boot behavior

    Without a handler, Spring Boot treats RuntimeExceptions as server errors and returns HTTP 500.
  2. Step 2: Check other options

    Ignoring exceptions or auto 404 responses require explicit handling; server crash does not happen.
  3. Final Answer:

    The application will return a 500 Internal Server Error response -> Option B
  4. Quick Check:

    Unhandled RuntimeException = HTTP 500 error [OK]
Hint: Unhandled RuntimeException returns HTTP 500 by default [OK]
Common Mistakes:
  • Assuming automatic 404 response
  • Thinking exceptions are ignored
  • Believing server crashes on exception
4. Identify the error in this custom exception class:
public class InvalidInputException extends RuntimeException {
  public InvalidInputException() {
    super();
  }
  public void InvalidInputException(String message) {
    super(message);
  }
}
medium
A. The class should extend Exception, not RuntimeException
B. The first constructor should accept a message parameter
C. The second method should be a constructor but is declared as void
D. There is no error; the class is correct

Solution

  1. Step 1: Check method declarations

    The second method has a return type void, so it is not a constructor but a normal method.
  2. Step 2: Understand constructor syntax

    Constructors must not have a return type; this causes the message constructor to be invalid.
  3. Final Answer:

    The second method should be a constructor but is declared as void -> Option C
  4. Quick Check:

    Constructor must not have return type = B [OK]
Hint: Constructors have no return type, not even void [OK]
Common Mistakes:
  • Adding void to constructor
  • Confusing methods with constructors
  • Extending wrong exception class
5. How can you handle a custom exception ResourceMissingException globally in Spring Boot to return a 404 status with a custom message?
hard
A. Create a @ControllerAdvice class with a method annotated @ExceptionHandler(ResourceMissingException.class) that returns ResponseEntity with status 404 and message
B. Throw ResourceMissingException inside a try-catch block and catch it in every controller method separately
C. Override the default error page in Spring Boot without using @ExceptionHandler
D. Use @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) on the exception class

Solution

  1. Step 1: Use @ControllerAdvice for global handling

    This annotation allows centralized exception handling across all controllers.
  2. Step 2: Define @ExceptionHandler method for ResourceMissingException

    This method can build a ResponseEntity with HTTP 404 and a custom message to send back.
  3. Final Answer:

    Create a @ControllerAdvice class with a method annotated @ExceptionHandler(ResourceMissingException.class) that returns ResponseEntity with status 404 and message -> Option A
  4. Quick Check:

    Global handler with @ExceptionHandler = A [OK]
Hint: Use @ControllerAdvice and @ExceptionHandler for global exception handling [OK]
Common Mistakes:
  • Catching exceptions in every controller method
  • Using wrong HTTP status annotation
  • Overriding error page without exception handler
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard