29

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.

1
  • 1
    What would you suggest as an alternative? Commented Aug 13, 2011 at 9:36

2 Answers 2

44

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.

Sign up to request clarification or add additional context in comments.

Comments

22

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
}

1 Comment

This is not correct. You don't need to extend IOException to create a checked exception. Every exception in Java is a checked exception unless it extends RuntimeException.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.