Java Custom Exceptions & Exception Hierarchy Quiz
30 in-depth questions (15 with code) covering creating custom exceptions, throw vs throws, checked vs unchecked exceptions, chained exceptions, and designing exception structures in Java.
Question 1
When should you create a custom exception class?
Question 2
What is the difference between 'throw' and 'throws'?
Question 3
What is a checked exception?
Question 4
What is an unchecked exception?
Question 5
What is exception chaining?
Question 6
How do you create a custom checked exception?
public class CustomCheckedException extends Exception {
public CustomCheckedException(String message) {
super(message);
}
public CustomCheckedException(String message, Throwable cause) {
super(message, cause);
}
}Question 7
How do you create a custom unchecked exception?
public class CustomUncheckedException extends RuntimeException {
public CustomUncheckedException(String message) {
super(message);
}
public CustomUncheckedException(String message, Throwable cause) {
super(message, cause);
}
}Question 8
What is the purpose of the cause parameter in exception constructors?
Question 9
When should you use checked exceptions in your API design?
Question 10
When should you use unchecked exceptions in your API design?
Question 11
What is the result of this custom exception creation?
public class ValidationException extends Exception {
private String fieldName;
public ValidationException(String fieldName, String message) {
super(message);
this.fieldName = fieldName;
}
public String getFieldName() {
return fieldName;
}
}
// Usage:
try {
throw new ValidationException("email", "Invalid email format");
} catch (ValidationException e) {
System.out.println("Field: " + e.getFieldName() + ", Error: " + e.getMessage());
}Question 12
What is wrong with this exception hierarchy design?
public class DatabaseException extends Exception {
// Generic database exception
}
public class ConnectionException extends DatabaseException {
// Connection problems
}
public class QueryException extends DatabaseException {
// Query execution problems
}
public class TimeoutException extends ConnectionException {
// Connection timeout
}Question 13
How do you properly chain exceptions?
try {
// Some operation that might fail
riskyOperation();
} catch (SQLException e) {
throw new CustomBusinessException("Business operation failed", e);
}Question 14
What is the output of this exception chaining example?
public class CustomException extends Exception {
public CustomException(String message, Throwable cause) {
super(message, cause);
}
}
public class Test {
public static void main(String[] args) {
try {
try {
throw new RuntimeException("Original error");
} catch (RuntimeException e) {
throw new CustomException("Wrapped error", e);
}
} catch (CustomException e) {
System.out.println("Message: " + e.getMessage());
System.out.println("Cause: " + e.getCause().getMessage());
}
}
}Question 15
Why should custom exceptions follow naming conventions?
Question 16
What is the problem with this custom exception?
public class MyException extends RuntimeException {
public MyException() {
// No message or cause
}
}Question 17
How should you design exception hierarchies for a library?
Question 18
What is the result of this throws declaration?
public void processData() throws CustomCheckedException {
if (dataInvalid) {
throw new CustomCheckedException("Data validation failed");
}
}Question 19
Why should you avoid creating too many custom exception types?
Question 20
What is the output of this exception hierarchy example?
class BaseException extends Exception {
BaseException(String msg) { super(msg); }
}
class SpecificException extends BaseException {
SpecificException(String msg) { super(msg); }
}
public class Test {
static void method() throws BaseException {
throw new SpecificException("Specific error");
}
public static void main(String[] args) {
try {
method();
} catch (BaseException e) {
System.out.println("Caught: " + e.getClass().getSimpleName());
}
}
}Question 21
How do you create an exception with additional context?
public class ValidationException extends Exception {
private final String field;
private final Object invalidValue;
public ValidationException(String field, Object invalidValue, String message) {
super(message);
this.field = field;
this.invalidValue = invalidValue;
}
// getters...
}Question 22
What is wrong with throwing built-in exceptions for custom errors?
Question 23
How should you handle exceptions when wrapping third-party libraries?
Question 24
What is the result of this chained exception access?
try {
throw new RuntimeException("Root cause");
} catch (RuntimeException e) {
CustomException ce = new CustomException("Wrapper message", e);
System.out.println("Wrapper: " + ce.getMessage());
System.out.println("Cause: " + ce.getCause().getMessage());
}Question 25
When should you make custom exceptions serializable?
Question 26
What is the problem with this exception design?
public class MyLibraryException extends Exception {
public MyLibraryException(String msg) { super(msg); }
}
public class MySpecificException extends MyLibraryException {
public MySpecificException(String msg) { super(msg); }
}
// But MySpecificException is thrown from places where MyLibraryException is declaredQuestion 27
Why should exception messages be meaningful?
Question 28
What is the output of this throws vs throw example?
public class Test {
static void method1() throws CustomException {
throw new CustomException("Error from method1");
}
static void method2() {
try {
method1();
} catch (CustomException e) {
throw e; // re-throwing
}
}
public static void main(String[] args) {
try {
method2();
} catch (CustomException e) {
System.out.println("Caught: " + e.getMessage());
}
}
}Question 29
How do you design exceptions for different layers of an application?
Question 30
Mastering custom exceptions means you now design APIs that:
