Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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& 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& e) {
            error = e.m_error;
        }
        hasTriedLoadingPath = true;
    }

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

    return muiPath;
}
share|improve this question
1  
bit of a moving target when you add to the question after somebody replied already. –  CyberSpock Jan 9 at 18:45
    
    
The added function follows the exact same pattern that I used in the first, and they really demonstrate the issue I have here. Sorry for the late addition. –  Felix Dombek Jan 9 at 18:54

1 Answer 1

up vote 2 down vote accepted

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& e) {
        error = e.m_error;

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.