aspose file tools*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Try catch block....." Watch "Try catch block....." New topic
Author

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....






output:

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





output:

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...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Try catch block.....