This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions in Java

 
Vaibhav G Garg
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two kinds of Exceptions in Java:

1. Checked
2. Unchecked.


I am curious to know what is the use of Unchecked Exceptions. These are the exceptions which programmer is not supposed to catch in the code or declare in the method signature and even compiler doesn't check for these exceptions. Then, why these exceptions have been provided in Java??
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34410
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A lot of the time you can't do anything about an exception. Suppose the database is down. Does every class in your app REALLY need to throw a SQLException? Instead, an unchecked exception lets you propagate it up to the top level and deal with "something unexpected" just the once.
 
Vaibhav G Garg
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:A lot of the time you can't do anything about an exception. Suppose the database is down. Does every class in your app REALLY need to throw a SQLException? Instead, an unchecked exception lets you propagate it up to the top level and deal with "something unexpected" just the once.


Thanks Jeanne!

So, what is the best approach to handle the Runtime (CHECKED) exceptions in java at different layers?
 
Abdulla S Mamuwala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compiler will force you to handle the checked exception.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaibhav G Garg wrote: . . . handle the Runtime (CHECKED) exceptions . . .
Runtime exceptions are not checked.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you have been through the Java Tutorials section?
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaibhav G Garg wrote:I am curious to know what is the use of Unchecked Exceptions. These are the exceptions which programmer is not supposed to catch in the code or declare in the method signature...

Actually, you're not required to, which is very different.

Then, why these exceptions have been provided in Java??

The truth is, more people ask: Why were checked Exceptions provided?

My answer is (and I know I'll get flamed by people who love checked Exceptions): I suspect that it's an idea that seemed good on paper at the time, but actually has little practical use. Even I like the idea of checked exceptions; but unfortunately their implementation - especially in a lot of java.io and java.sql classes - falls short of what I suspect was the intent.

However, Campbell's quite right: You should read the tutorials ... although even they might not answer all your "why" questions.

Winston
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope you might have come across the dreaded Blue Screen error's of windows OS, they are the classic examples of unchecked exceptions thats why they have been rightly termed as BSOD's
 
Vaibhav G Garg
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Vaibhav G Garg wrote: . . . handle the Runtime (CHECKED) exceptions . . .
Runtime exceptions are not checked.


YES it is UNCHECKED only. It was a typo.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . read the tutorials ... although even they might not answer all your "why" questions.

Winston
They try their hardest to answer the questions, but come down heavily towards supporting checked Exceptions.
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding of unchecked exceptions is that it informs that there is some thing fatally wrong with code which needs to be taken care of. Checked exceptions, on other hand, represent those scenarios where user may have provided some wrong data,like non-existing file, and hence killing a program due to wrong user input doesn't make sense.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manish ghildiyal wrote:My understanding of unchecked exceptions is that it informs that there is some thing fatally wrong with code which needs to be taken care of. Checked exceptions, on other hand, represent those scenarios where user may have provided some wrong data,like non-existing file, and hence killing a program due to wrong user input doesn't make sense.

Yup, that was certainly the intent; but it's been ruined down the years by lazy implementations that throw "amorphous" things like IOException and SQLException (both of which are checked) willy-nilly.

What am I supposed to do if a database is down, or I get a disk read error when accessing a file?

For starters, I don't even know that that's what happened, since nobody bothered to create a DatabaseIsDownException or DiskReadError (the latter of which arguably shouldn't be checked anyway); and once I've got past the point of establishing a connection to a database, the rest of the API should either:
(a) Work.
(b) Throw an Error (eg, if a network connection goes down).
(c) Throw an Exception that indicates a logic flaw (eg, a badly-formed piece of SQL); and IMO that's still a runtime error - because it indicates a bug - and so probably shouldn't be checked either.

One of the things that tells me that many of these APIs are flawed is the recent appearance of the java.nio package, which contains lots of goodies for avoiding IOException. I wonder when java.nsql will appear?

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic