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.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Unchecked vs Checked Exception

 
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?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • 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 Windows XP Java
  • 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
 
Sheriff
Posts: 21842
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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.
 
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!