This week's book giveaways are in the Jython/Python and Object-Oriented programming forums. We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line! See this thread and this one for details.
anish jain wrote:But actually if i want to make an user defined exception then in that case how to decide whether to go for checked or unchecked?
again, for what purpose you are creating user defined exception? suppose, you are developing one software module/layer,
1.do you want to force your client (who may be your colleague, developing a software on base of your module) to handle the exception which you are throwing from your module? if yes then go for checked exception.
2. or you want to throw an exception which indicates your software problem that should logged in to your module itself for find out the problem/debugging, then go for unchecked exception.
does that make sense?
posted 8 years ago
yes it does make sense..i understood it theoretically..but if you can state your points with some examples(giving some code examples) it would clarify in a more better way
Please SearchFirst. This question has been asked and answered more times than I can remember. If you didn't find anything through Google that means you haven't used the right search words, because this site would have definitely had many hits.
There is no fixed and easy to remember rule that tells you whether you should use a checked or an unchecked exception. Have a look at what this tutorial page has to say about it:
The Java Tutorials wrote:
The first kind of exception is the checked exception. These are exceptional conditions that a well-written application should anticipate and recover from. ...
The third kind of exception is the runtime exception. These are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from. These usually indicate programming bugs, such as logic errors or improper use of an API.
You have to decide for yourself which of these is most appropriate in your case.
Note that the standard Java library itself isn't really consistent with regard to this. Some parts of the library throw checked exceptions, that had probably better be defined as unchecked exceptions (one example that comes to mind is the XML parsing package - javax.xml.parsers.ParserConfigurationException is a checked exception, but it should have been an unchecked exception in my opinion).
Some people even think that the whole idea of checked exceptions is wrong and that Java shouldn't have had this whole concept in the first place.