Error checking in function that returns reference to static variable This is a function I just wrote with the goal to only load the desired information once, and if an error occurs, save it for later so that it can always be reported to a calling function.

It does what I want but seems quite clumsy.

std::wstring const&amp; GetMuiFilePath()
{
    static DWORD error = NO_ERROR;
    static std::wstring muiPath;
    static bool hasTriedLoadingPath = false;
    if (!hasTriedLoadingPath) {
        try {
             muiPath = Internal::zGetSpecialFolder(zCsidlSystem)
                     + L"\\" + Internal::zGetSystemLocale()
                     + L"\\" + L"wbadmin.exe.mui";
        }
        catch (AutoWinError const&amp; e) {
            error = e.m_error;
        }
        hasTriedLoadingPath = true;
    }

    if (muiPath.empty()) {
        throw AutoWinError(error);
    }

    return muiPath;
}
The only things I would change are:

    // Move this boolean to the top.
    // So if you throw an exception that is not caught
    // you then stil know that you have tried.
    hasTriedLoadingPath = true;


    try {
         // No change.
         muiPath = Internal::zGetSpecialFolder(zCsidlSystem)
                 + L"\\" + Internal::zGetSystemLocale()
                 + L"\\" + L"wbadmin.exe.mui";
    }
    catch (AutoWinError const&amp; e) {
        error = e.m_error;

        // Curious why you did not re-do the throw here?
        throw;  // Note. Not  `throw e;`
                // Rethrows the current exception.
    }