Chapter 12: Problem 19
If you are writing a custom exception class, how can you make sure it is checked? How can you make sure it is unchecked?
Short Answer
Expert verified
Answer: The two types of exceptions in Java are checked and unchecked exceptions. Checked exceptions are those that are checked at compile-time and require explicit handling using try-catch blocks or the throws keyword. Unchecked exceptions are runtime exceptions and do not require explicit exception handling.
To create a custom checked exception class, you need to extend the java.lang.Exception class, like this:
```java
public class CustomCheckedException extends Exception {
public CustomCheckedException(String message) {
super(message);
}
}
```
To create a custom unchecked exception class, you need to extend the java.lang.RuntimeException class, like this:
```java
public class CustomUncheckedException extends RuntimeException {
public CustomUncheckedException(String message) {
super(message);
}
}
```