close file when error occurs