Move assignment of std_handles out of try loop so that if this fails we don't try an erroneous restore.