Added proper stream closing when an exception occured when output file is opening