Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Explanation required(exception handling)?????

 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,


try to compile this code you get compilation error which is justifiable....

now try this



see the code compiles-runs just fine!!!


i'm unable to understand this ..can anyone help me understanding this??

i 'll be really gr8ful...

thanx
amit
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I compiled both of your code samples and both of them compiled just fine with JDK 1.4.

So can you please double check it?
 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ooooooooops........!!!

sorry guys for this silly mistake it was a typo first one is just-fine as it catches a RuntimeException, so No Problemo!!)

actually i wanted to post this instead Replace first code with this plz)


thanks buddy fpr pointing out....actually i was just trying out sth and i just posted that instead of what i actullay found bewildering

thanx
amit
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, in this second example your code does not compile because your trying to catch a checked exception that could never occur in you code
 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well why does the second code compiles fine....where i catch an Exception.........a checked exception will always be an instanceof Exception...right....so why only catching a checked exception, which can never be thrown by a method, behaves differently???

thanx
amit
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think: what other kind of exception is a subclass of Exception? Look at the API.
[ May 14, 2005: Message edited by: Barry Gaunt ]
 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i knew someone will say that, but is that reason good enough to support that...suppose: if the second code failed to compiled fine.....you can again say, it does so as an exception object can be of type checked one so compiler cribs......infact to me that sounds more logical......

thanx
amit
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so what is the final answer ?

code does not compile because your trying to catch a checked exception that could never occur in you code ??

is it the correct and final answer ?

and what if have several catch block and if one of those are useless(never to be caught)
will even then the compile time errror will occour...

pls clarify my doubt and give the final answer...

i got confused with the last Amit das post....
...

 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are subclasses of Exception which are not checked. The compiler is allowing you to catch that type of exception.
 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but is it a gud practice to catch an unchecked(not-checked-by-compiler)exception???

thanx
amit
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good practices are not always enforced by the compiler. You may want to catch a NumberFormatException and present a nice error message to your user, for example. A NumberFormatException is a RuntimeException which is unchecked, but it is still a subclass of Exception.
[ May 14, 2005: Message edited by: Barry Gaunt ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Amit]: but is it a gud practice to catch an unchecked(not-checked-by-compiler)exception???

It certainly can be a good practice. It depends on the specific situation I suppose. Sometimes it's good, sometimes not. But there's a popular myth in some circles that catching an unchecked exception is always bad, and this idea is wrong, wrong, wrong. Barry's example of catching a NumberFormatException is good - it's a perfectly legitimate thing to do.
[ May 14, 2005: Message edited by: Jim Yingst ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic