Bird
Raised Fist0
Spring Bootframework~10 mins

Custom exception classes in Spring Boot - Interactive Code Practice

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

Complete the code to declare a custom exception class that extends RuntimeException.

Spring Boot
public class MyException extends [1] {
    public MyException(String message) {
        super(message);
    }
}
Drag options to blanks, or click blank then click option'
AException
BRuntimeException
CThrowable
DError
Attempts:
3 left
💡 Hint
Common Mistakes
Extending Exception instead of RuntimeException causes checked exceptions.
Extending Throwable or Error is not recommended for custom exceptions.
2fill in blank
medium

Complete the code to throw the custom exception with a message.

Spring Boot
if (user == null) {
    throw new [1]("User not found");
}
Drag options to blanks, or click blank then click option'
AException
BIllegalArgumentException
CMyException
DRuntimeException
Attempts:
3 left
💡 Hint
Common Mistakes
Throwing a generic Exception instead of the custom one.
Using a different exception class that does not match the custom exception.
3fill in blank
hard

Fix the error in the custom exception constructor to properly pass the message to the superclass.

Spring Boot
public class MyException extends RuntimeException {
    public MyException(String message) {
        [1](message);
    }
}
Drag options to blanks, or click blank then click option'
Asuper
Bthis
Cthrow
Dreturn
Attempts:
3 left
💡 Hint
Common Mistakes
Using this(message) instead of super(message).
Trying to use throw or return inside constructor.
4fill in blank
hard

Fill both blanks to create a custom exception with a default message and a constructor that accepts a message.

Spring Boot
public class [1] extends RuntimeException {
    public [2]() {
        super("Default error message");
    }
}
Drag options to blanks, or click blank then click option'
AMyException
BCustomException
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for class and constructor.
Using a generic name that does not match the class.
5fill in blank
hard

Fill all three blanks to define a custom exception with two constructors: one default and one with a message.

Spring Boot
public class [1] extends RuntimeException {
    public [2]() {
        super("Default message");
    }
    public [3](String message) {
        super(message);
    }
}
Drag options to blanks, or click blank then click option'
ACustomError
BCustomException
CMyException
DAppException
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for constructors and class.
Mixing class names from other examples.

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