Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Checked and Unchecked exceptions, when to use each one?

807589Aug 17 2008 — edited Aug 18 2008
Suppose you want to extend either Exception or RuntimeException (not Error) and create a new exception for your API or application. When do you use each type of exceptions?

I mean, which exceptions should be checked, and which ones should be unchecked?
It's not possible to express a general rule, but some guidelines may help.

For example suppose that you want to parse a String inside a "fromString" method and create a new instance of an object. Which one do you choose for this method to throw when the passed String is not in a correct form:

1. IllegalArgumentException or a subclass of it, which are unchecked.

2. A new checked exception to force the programmer to handle the situation.

Any suggestion would be appreciated.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 15 2008
Added on Aug 17 2008
19 comments
443 views