0

i'm writing some exception handling for some C++ MFC code.

I'm facing an issue where my outer exception "UserNotExists" is being caught but the inner exception raises an "Exception Unhandled" error. Can anyone help me to understand why the second exception "WrongPasswordException" isn't being captured by the try catch block?

Context: The function calls the API class with login details from the GUI. The API will check the database to see if the user exists and if the password is correct and throw exceptions accordingly.

Edit: I should add that g_UserAuthenticationApi is a global variable of a DLL export.

//#CMFC.cpp
void CMFCDlg::OnBnClickedOk()
    try
    {
        g_UserAuthenticationAPI->TryLogin(std::wstring(m_Initials), std::wstring(m_Password));
        OnOK();
        
    }
    catch (std::exception& e)
    {
        //oops
        AfxMessageBox(CString(e.what()));
    }
//# UserAthenticationAPI.cpp
bool UserAuthenticationAPI::TryLogin(std::wstring szInitials, std::wstring szPassword)
{
    if (!m_AuthRepo->DoesUserExist(szInitials))
        throw UserNotExistsException("User initials not found");

    auto sb64 = m_HashingRepo->Hash(szPassword);

    if (!m_AuthRepo->LoginR(szInitials, sb64))
        throw WrongPasswordException("Incorrect Password.");

    m_fLoggedIn = true;
    m_szInitials = szInitials;

    return true;
}
//#UserAuthenticationEceptions.h
// These are all the same...
class UserExistsException: public std::runtime_error
{
public:
    UserExistsException(const std::string& msg) : std::runtime_error(msg) {}
};

  • 2
    Try `catch (const std::exception& e)` – πάντα ῥεῖ Oct 06 '22 at 02:54
  • The exception is still unhandled unfortunately – user2279376 Oct 06 '22 at 03:44
  • Try: `catch(...)` to catch all the exceptions – Asesh Oct 06 '22 at 03:51
  • 1
    Using exceptions for control flow is rather unusual. – IInspectable Oct 06 '22 at 05:54
  • @Asesh even with `catch(...)` the second exception is unhandled – user2279376 Oct 06 '22 at 06:04
  • @IInspectable , Can you suggest a preferential approach ? returning an error code enum maybe? – user2279376 Oct 06 '22 at 06:06
  • 1
    `TryLogin` returns a `bool` value already. That's what (presumably) communicates error or success to the caller. Throwing exceptions from its implementation only means that callers will now have to handle errors in *two* different ways. Just return the outcome. If you need more error information passed to callers, return a structure of your choosing. – IInspectable Oct 06 '22 at 06:15
  • Do I understand you right: You mean m_AuthRepo-LoginR(szInitials,..) throws an exception? – Tom Tom Oct 06 '22 at 15:57
  • @TomTom No the exception is thrown when LoginR returns false, since the exception cannot be caught even with `catch(...)` i'm thinking this has something to do with MFC and being a DLL export. It is strange however that the second exception, regardless of what i call it, or if i throw them both as std::runtime_error ect cannot be caught... – user2279376 Oct 06 '22 at 22:31
  • A note from MVP: Throwing C++ exceptions across DLL boundaries is only possible when all modules use the same C++ runtime, in which case they share a heap as well. But this can be a maintenance burden, especially when libraries from multiple vendors are involved, so it is discouraged. [link] (https://stackoverflow.com/a/5108118/4303373) – Tom Tom Oct 07 '22 at 23:59

0 Answers0