• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems with try block  RSS feed

 
Billy Sang
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I cannot figure out how to deal with the scope of out PrintWriter object. If I delcare it outside, letting it be visible to the while loop. The compiler returns an error saying I need to use a try block to cath its exceptions, which I am aware of. However, the out object then becomes invisible to the while loop and alos the finally block for close the out object. Any help?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The usual approach is something like this:



 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declare the "out" variable in a scope which is visible to all code which needs to use it. If you get compiler errors when you do that, and you can't figure out what they mean, then tell us what they are. And I don't mean a vague description of what you think they are saying, I mean copy them and paste them here.
 
Billy Sang
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works now after I implemented jeffs code. Okay thanks to both of you for the help.
 
Billy Sang
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another question: Which do you think is more suitable having the arguments be passed as File objects or String objects?
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're going to call the method "reverseFile" then I would definitely lean towards passing File references as its parameters. You could of course also have an overloaded method which took String references and created Files from them before delegating to your original method.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:The usual approach is something like this:




And if you're using Java 7 you can make this a lot simpler with the try-with-resources statement:

Note that the PrintWriter will automatically be closed when you use this syntax; you don't need the finally-block with the complicated nested exception handling.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if you are not using Java7 and try with resources, Rob Spoor points out there is a simpler way to write that finally block.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Even if you are not using Java7 and try with resources, Rob Spoor points out there is a simpler way to write that finally block.


That's no good. We don't want an exception in finally to change how the try statement completes. If there was an exception reading the file, we want that one to come out of the try statement, not the one that occurred cleaning up. And if there was no exception--that is, if the try block did its job successfully--we don't want the cleanup code to prevent us from getting that result.

Never throw, return, break, or continue out of finally.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!