Forums Register Login

Problems with try block

+Pie Number of slices to send: Send


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?
+Pie Number of slices to send: Send
The usual approach is something like this:



+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
It works now after I implemented jeffs code. Okay thanks to both of you for the help.
+Pie Number of slices to send: Send
Another question: Which do you think is more suitable having the arguments be passed as File objects or String objects?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
got it.
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 787 times.
Similar Threads
Send a String to Server and server send it somewhere on network
Why am I getting a Null Pointer Exception???
Java And OpenDocument Files
Getting output to work
Help with writing to a file!
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 00:28:23.