The thing is, the compiler doesn't know that System.exit will close the JVM, so it sees that there is a flow path where you never have initialized ostream - if the construction of the FileOutputStream or DataOutputStream fails, the catch block is executed and the loop starts (according to the compiler at least).
By returning in the catch block, the compiler sees that if the loop starts, the try block executed successfully and ostream has a value.
As often happens, there are several threads with rather similar problems, so I showed an example of such a try yesterday here. Note that I realised today I had made a mistake ( ), so you need to read two of my posts.
Brian Legg wrote:I usually put all of my code inside the try block . . .
Campbell Ritchie wrote:Why are you using DataOutputStream for a text file? Writers would be easier to use: look at the Java™ Tutorials section.
This was an academic exercise, came straight from a textbook I'm reviewing.
I condenced it a bunch but changed the structure to show what I meant. As you can see at any point in the code an Exception is thrown the code will immediately be sent to the end of your method where your resources will be closed and the program will end.
That's what we are here for
Patrick Brooks wrote:
OK, thanks for the tip! These are things they don't teach you in school!
By the way: Please use the buttons on the left for smilies; some of them use unusual code, and some of the usual code eg ;) doesn't work. That is because some code (eg SQL links) can have a ; immediately followed by a )