• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unchecked vs Checked Exception  RSS feed

 
anish jain
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a bit confused about when to use Checked and Unchecked Exceptions?
I have tried enough on Google but couldn't get any satisfactory answer.

Could you please help me understanding the difference between these two with the help of an example?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I uses,

Checked Exception : I throw them to show the user messages.

Unchecked Exception: runtime exception, occurred due to logic error/programmer mistake, so typically I log them for debugging purpose.after thorough testing it should not occur.

Again, every programmer have their own style in this regards.
 
anish jain
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ..

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?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
anish jain
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

So, there are no exact answers to this question.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!