Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a custom exception class in Java?
A custom exception class is a user-defined class that extends the Exception class (or one of its subclasses) to create specific error types for your program.
Click to reveal answer
beginner
How do you declare a custom checked exception in Java?
You create a class that extends Exception and provide constructors, usually calling super() to pass messages or causes.
Click to reveal answer
beginner
Why would you create a custom exception class?
To represent specific error conditions in your program clearly, making error handling easier and your code more readable.
Click to reveal answer
intermediate
What is the difference between checked and unchecked exceptions in Java?
Checked exceptions must be declared or handled; they extend Exception. Unchecked exceptions extend RuntimeException and don't require explicit handling.
Click to reveal answer
beginner
Show a simple example of a custom exception class in Java.
public class MyException extends Exception {
public MyException(String message) {
super(message);
}
}
Click to reveal answer
Which class should you extend to create a checked custom exception in Java?
AError
BException
CRuntimeException
DThrowable
✗ Incorrect
Checked exceptions extend Exception but not RuntimeException.
What keyword is used to throw a custom exception in Java?
Athrows
Bcatch
Cthrow
Dtry
✗ Incorrect
The 'throw' keyword is used to actually throw an exception object.
Which constructor is commonly called inside a custom exception class constructor?
Asuper()
Bthis()
Cnew Exception()
Dparent()
✗ Incorrect
super() calls the parent Exception class constructor.
If you want an exception that does NOT require explicit handling, which class should you extend?
ARuntimeException
BException
CError
DThrowable
✗ Incorrect
RuntimeException and its subclasses are unchecked exceptions.
What is the purpose of creating a custom exception?
ATo replace all built-in exceptions
BTo avoid using try-catch blocks
CTo speed up program execution
DTo represent specific error conditions clearly
✗ Incorrect
Custom exceptions help make error handling clearer and more specific.
Explain how to create a custom checked exception class in Java and why you might need one.
Think about extending Exception and passing messages.
You got /4 concepts.
Describe the difference between checked and unchecked exceptions and how this affects creating custom exceptions.
Focus on exception hierarchy and handling requirements.
You got /4 concepts.
Practice
(1/5)
1. What is the correct way to start creating a custom exception class in Java?
easy
A. Extend the Exception or RuntimeException class
B. Implement the Exception interface
C. Create a class with the same name as Exception
D. Use the throw keyword in the class declaration
Solution
Step 1: Understand Java exception hierarchy
Custom exceptions must extend either Exception or RuntimeException to behave like exceptions.
Step 2: Recognize correct inheritance
Implementing an interface or naming a class Exception does not create a proper exception class.
Final Answer:
Extend the Exception or RuntimeException class -> Option A
Quick Check:
Custom exception = extends Exception [OK]
Hint: Always extend Exception or RuntimeException for custom exceptions [OK]
Common Mistakes:
Trying to implement Exception as an interface
Naming class Exception instead of extending it
Using throw keyword in class declaration
2. Which of the following is the correct constructor for a custom exception class named MyException?
easy
A. public MyException() { this.message = message; }
B. public void MyException(String message) { super(message); }
C. public MyException(String message) { super(message); }
D. public MyException(String message) { print(message); }
Solution
Step 1: Identify correct constructor syntax
Constructors have no return type and call super(message) to pass the message to the parent Exception class.
Step 2: Check each option
public MyException(String message) { super(message); } correctly defines a constructor calling super(message). public MyException() { this.message = message; } incorrectly assigns message without declaration. public void MyException(String message) { super(message); } has a void return type, so it's not a constructor. public MyException(String message) { print(message); } calls a non-existent method print.
Final Answer:
public MyException(String message) { super(message); } -> Option C
public class InvalidDataException extends RuntimeException {
public InvalidDataException(String message) {
super(message);
}
} correctly extends RuntimeException with proper constructor. public class InvalidDataException extends Exception {
public InvalidDataException(String message) {
super(message);
}
} creates a checked exception. public class InvalidDataException implements RuntimeException {
public InvalidDataException(String message) {
super(message);
}
} tries to implement an exception class, which is invalid. public class InvalidDataException extends Throwable {
public InvalidDataException(String message) {
super(message);
}
} extends Throwable directly, which is not recommended for custom exceptions.
Final Answer:
public class InvalidDataException extends RuntimeException {
public InvalidDataException(String message) {
super(message);
}
} -> Option B