my dog learned polymorphism
The moose likes Features new in Java 7 and the fly likes try-with-resources Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Features new in Java 7
Bookmark "try-with-resources" Watch "try-with-resources" New topic
Author

try-with-resources

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i really like this new feature. when you do IO you only need try and catch. not try, catch, and finally(to close).


SCJP
Visit my download page
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3995
    
  18

Actually, you don't even need a catch. You can just use the try clause.


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 20080
    
  30

The catch rules haven't changed - you either catch or declare it in a throws clause. All try-with-resources provides is a finally block that closes all resources declared in the "header".


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3995
    
  18

If the object is an AutoCloseable that doesn't throw any checked exceptions, you don't need to declare anything at all

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 20080
    
  30

Except the Scanner, which is what I was referring to. And the close() method can throw any exception it wants to, as it's suppressed by the try-with-resources block.
 
Have you checked out Aspose?
 
subject: try-with-resources
 
It's not a secret anymore!