How does RAII help when a function throws?
Resource acquisition is initialization (RAII) ties cleanup to an owning object’s lifetime. If an exception unwinds the stack after a std::lock_guard has acquired a mutex, the guard’s destructor releases it. An early return also destroys the local guard. You avoid having to put a matching unlock on every exit path.
Follow-up: Who owns a resource returned by a factory? Use std::unique_ptr when the caller becomes its sole owner. Use std::shared_ptr only if independent owners need to keep it alive. The C++ Core Guidelines on resource management cover RAII and when to prefer unique ownership.

