Make sure cached file is deleted if exception thrown during write

Added catch blocks for exceptions thrown during file write.  Note the repetitive code...  The writer close was pulled out of the finally and repeated in the try and both catch blocks.  This repetition is needed so that the writer close can be called before a file delete in the catch (we dont't want to delete in catch then close in finally)  The multiple catch blocks are so that the exceptions are properly typed for (re)throw.