aspose file tools*
The moose likes Beginning Java and the fly likes Exceptions in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exceptions in Java" Watch "Exceptions in Java" New topic
Author

Exceptions in Java

Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 140
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30309
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 140
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

Joined: Jul 27, 2009
Posts: 21
Compiler will force you to handle the checked exception.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Vaibhav G Garg wrote: . . . handle the Runtime (CHECKED) exceptions . . .
Runtime exceptions are not checked.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
I presume you have been through the Java Tutorials section?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7652
    
  19

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

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


Still a beginner at crafting quality coding but have the zeal to learn more. Your help would be appreciated.
My Blog on Learning Analytics
Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 140
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

Joined: Oct 13, 2005
Posts: 38363
    
  23
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

Joined: Jan 12, 2013
Posts: 136
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

Joined: Mar 17, 2011
Posts: 7652
    
  19

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exceptions in Java