exit loop if an error occurred when writing to stream