Checked and Unchecked exceptions, when to use each one?
807589Aug 17 2008 — edited Aug 18 2008Suppose 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.