This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

try-with-resources

 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i really like this new feature. when you do IO you only need try and catch. not try, catch, and finally(to close).
 
Stephan van Hulst
Bartender
Posts: 6322
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, you don't even need a catch. You can just use the try clause.
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Stephan van Hulst
Bartender
Posts: 6322
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic