This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Try catch block..... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Try catch block....." Watch "Try catch block....." New topic

Try catch block.....

Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
i didn't understand why there is compiler error in the second case....


No compiler error. The lines "Before Try" and "At the end" are printed on the screen.


Compiler error complaining about the catch block where no IOException object can ever be thrown.
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
The compiler doesn't mind if you try to catch Exception or Throwable because what you might be catching is an unchecked exception that the compiler is not going to look for in your code. For example, something in the try block might throw a RuntimeException or an Error which would be caught by catch (Throwable t). The compiler doesn't care whether there's anything in the try block that could throw either of those. It just assumes that it's possible.

However, when you specify that you are catching something that can only be a checked exception, such as IOException, then the compiler will only allow it if it actually might be thrown by code in the try block.
[ May 05, 2005: Message edited by: Joe Sanowitz ]

SCJA 1.0 (98%), SCJP 1.4 (98%)
Jas Oberai
Ranch Hand

Joined: Jan 16, 2005
Posts: 231
Joe,can you please explain this with some code,i toowant to clarify my doubts!!

SCJP 1.4 (88%)<br />SCWCD 1.4 (88%)
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi joe

let me tell what i understand

if we are cathching only those exceptions subclass to Exception class and too not RuntimeException, then only the compiler will look for the code inside the try block..

my doubt is
for instance if i am catching IOException then is it mandatory for us to put some code relating to some I/O operations inside the try block...
I agree. Here's the link:
subject: Try catch block.....
It's not a secret anymore!