When I create my own exceptions, is it possible to mark them as being checked/unchecked? (using some annotation, maybe?) Or, is extending Exception/RuntimeException the only way of doing it?
Thanks.
When I create my own exceptions, is it possible to mark them as being checked/unchecked? (using some annotation, maybe?) Or, is extending Exception/RuntimeException the only way of doing it?
Thanks.
The only way of doing it is to extend Exception (or a subclass thereof) for a checked exception, and extending RuntimeException (or a subclass thereof) for an unchecked exception.
Given how lightweight it is to do that, and the benefits you get from extending those classes, I don't think that's too onerous.
Checked Exception
By extending Exception, you can create a checked exception:
class NotEnoughBalance extends Exception {
// Implementation
}
Unchecked Exception
By extending RuntimeException, you can create unchecked exception:
class NotEnoughBalance extends RuntimeException {
// Implementation
}
IOException to create a checked exception. Every exception in Java is a checked exception unless it extends RuntimeException.