File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Catching exceptions 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 "Catching exceptions" Watch "Catching exceptions" New topic

Catching exceptions

Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162

I didn't get why this code compiles:

And this one not:

[ July 26, 2005: Message edited by: George Bolyuba ]

SCJP 1.4 (100%) Done.<br />SCJD (URLyBird 1.2.3 Started)
Jaikiran Pai

Joined: Jul 20, 2005
Posts: 10441

Whats the compilation error

[My Blog] [JavaRanch Journal]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I'll try:

In your first example you are catching all Exceptions. Some of those exceptions could be unchecked RuntimeExceptions. Runtime exceptions can be thrown from the JVM and apparently "appear from nowhere". Rather unlikely, but theoretically possible. So the compiler has to allow for the code to compile.

In your second example you are catching all checked IOExceptions and the compiler can see that there are none being thrown or declared to be thrown by the code in the try. So the compiler can rightly refuse to compile the code.

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

Joined: Feb 18, 2005
Posts: 162
Thanks, Barry. It sounds reasonably.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1599
Hi Barry!
Hope I am not misunderstanding you. Do you mean... compiler is intelligent enough to check "if there is a catch, there should be some reason/code which can throw it." ???
And if it doesnt find any such reason/code it will simply say "You have a catch for no reason...i dont find it useful!!!" :roll:

Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 812
yes compiler is intelligent because its made by intelligent ppl

Thanks and Regards, Amit Taneja
Satish SN
Ranch Hand

Joined: Apr 19, 2005
Posts: 70
hi george,

I feel in first code u are catching the Exception class which means it could throw any of the exceptions so compiler need not worry about what exception it is.

in the second case u r specifically mentioning about IOException so u r narrowing down to the specific type of exception so the compiler intelligently looks for the code which for example throws IOException it doesnt find any so it throws compiler error.

Satish SN<br />SCJP 1.4 & SCWCD 1.4
Arulkumar Gopalan
Ranch Hand

Joined: Oct 13, 2003
Posts: 104
Just to add what Kumar satish said above:

second case:
Code should perform an IO operation with in try block which might throw an IOException, but not neccessarily using the keword throws.

Anbudan & Mahalo,<br />Arul<br /> <br />-Not a sun certified Java professional :-)
I agree. Here's the link:
subject: Catching exceptions
It's not a secret anymore!