0
0
Spring Bootframework~10 mins

Custom exception classes in Spring Boot - Interactive Code Practice

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