2
\$\begingroup\$

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;
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Jan 9, 2015 at 16:48
\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

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.
 }
answered Jan 9, 2015 at 17:59
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.