Domanda

Fun exploring some legacy code today. Ran into this little number:

function Func1()
{
DWORD dwError;
try
{
    dwError = 1;
    throw "Hey!";
} catch (LPCTSTR szError)
{
    Log("Log1: %d", dwError);
    SetLastError(dwError);
    throw szError;
}
}

function Func2()
{
    try {
       Func1();
    } 
    catch (LPCTSTR szError)
    {
         DWORD dwLastError = GetLastError();
         Log("Log2: %d", dwLastError); ///OMG is 0!
    }
}

GetLastError() returns 0! Why is that? The functions are actually a bit more complicated than this. They do include a few things on the stack (DWORDs, CString, BYTE[]). What should I be looking for?

Logs look like:

Log1: 1

Log2: 0

È stato utile?

Soluzione

C++ exceptions in the MSVC compiler and runtime are built on top of native Windows SEH. Stack unwinding is actually performed by Windows. Using Windows api functions is going to affect the value stored for GetLastError(). More details about the connection with SEH in this answer.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top