aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Explanation required(exception handling)????? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Explanation required(exception handling)?????" Watch "Explanation required(exception handling)?????" New topic
Author

Explanation required(exception handling)?????

Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
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

Joined: Dec 31, 2004
Posts: 961
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

Joined: Mar 05, 2005
Posts: 206
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

Joined: Dec 31, 2004
Posts: 961
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

Joined: Mar 05, 2005
Posts: 206
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

Joined: Aug 03, 2002
Posts: 7729
Think: what other kind of exception is a subclass of Exception? Look at the API.
[ May 14, 2005: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
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

Joined: Mar 14, 2003
Posts: 810
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....
...



Thanks and Regards, Amit Taneja
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
There are subclasses of Exception which are not checked. The compiler is allowing you to catch that type of exception.
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
but is it a gud practice to catch an unchecked(not-checked-by-compiler)exception???

thanx
amit
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Jan 30, 2000
Posts: 18671
[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 ]

"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Explanation required(exception handling)?????