The issue is that the stream object created and used in the try block is not directly accessible in the catch block. In Developer, it looks like you’re passing in the stream object to close but at run-time it isn’t in the pipeline directly.
There are two approaches that you can use.
Declare the stream object outside of the try block. Assign it a null value. Then it will be available directly in the catch block, with the stream var updated as appropriate from open/write.
In the catch block, variables that were in the pipeline at the time of the exception are stored in the lastError/pipeline. You can pass lastError/pipeline/stream to the close service.
A 3rd possibility might be to lump all this file interaction into a single Java service. Normally I’m an advocate of implementing things in FLOW but this is one of those cases where the granularity of the PS Java services may be too fine.
If you just need to write some data to a file you might consider using a writeFile service that opens, writes, closes/deletes on error. There are a couple of these posted in the forums and in the Shareware section I think (they probably don’t delete on error but you can add that). You can also find one at my blog which you can access via the link in my signature block below. Start with one of these and modify as you see fit.