I found couple of posts similar to mine, but didn't find a solution.
I am trying to write to a file and if it fails, i want to delete it.
Issue: As the file was not properly closed, i am not able to delete it (in my catch block). I tried to close the file with proper input (in catch block) but it throws an exception. I am using services from PSUtils
Any help would be appreciated.
Can you post the service steps or the Java code being used? Perhaps we can see what needs to be tweaked to close the file on failure so it can be deleted.
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.