Added on_scope_exit for exception safety.