More careful exception handling in stream code