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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.