Catch and exception while writing stream