Better handling of exceptions in flush()