Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

try-with-resources  RSS feed

 
Randall Twede
Ranch Hand
Posts: 4690
8
Java 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
Saloon Keeper
Posts: 7807
142
  • Likes 1
  • 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
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • 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
Saloon Keeper
Posts: 7807
142
  • 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
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • 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
Boost this thread!