What is a checked exception? How is it different from an unchecked exception?
A checked exception is an exception that must be either caught or declared in the method signature using throws
. These exceptions are typically used for recoverable conditions, like IOException
.
An unchecked exception (also known as a runtime exception) does not require explicit handling or declaration. They usually indicate programming bugs, such as NullPointerException
.
Key difference:
- Checked exceptions are enforced by the compiler to be handled.
- Unchecked exceptions are not required to be handled or declared.