views:

79

answers:

2

Hello all, I've got a stange situation in visual studio 2008 C++. I work on code that was originally written for visual studio 2003, where everything works well. Now, ported to VS 2008, the exception handling, which unfortuantely exists widely in the code, does not work anymore. standard code example:

 try
 {
      HRESULT hr = S_OK;
      // do stuff...
      if( FAILED( hr ) ) 
          throw hr;
 }
 catch( HRESULT hr )
 {
      // error handling, but we never get here
 }
 catch( ... )
 {
      // ... not even here
 }

Under VS 2008, no exception is encountered, but I get a crash somewhere else, indicating that the stack pointer must be screwed up. Did anybody come across this behaviour? Any help is appreciated.

+1  A: 

I get a crash somewhere else, indicating that the stack pointer must be screwed up.

How so? The fact of a crash has nothing to do with the stack. The bigger issue here is that we don't know what "hr" was declared as. If it was declared anything other than HRESULT, the compiler does not need to catch there.

Specifically, I believe HRESULT's definition changed with VS2005 in order to support 64 bit windows. If hr is declared as something else that happened to be the same as HRESULT before, but is not after you installed the new Windows SDK, then that's a likely cause.

Can't say more without seeing more code.

EDIT: The following works correctly:

#include <iostream>
#include <iomanip>
#include <windows.h>

int main()
{
    try
    {
        HRESULT hr = E_FAIL;
        std::cout << "Inside try\r\n";
        if( FAILED( hr ) ) 
            throw hr;
    }
    catch( HRESULT hr )
    {
        std::cout << "Error:" << std::hex << (unsigned int)hr;
    }
    system("pause > nul");
}

We need to see more code.

Billy ONeal
It is declared as HRESULT, so both declarations are the same -
arionik
@arionik: Then we need to see more code.
Billy ONeal
@Billy: your code works fine for me too under VS2008 ... My guess is that its not throwing for some reason
Goz
+1  A: 

After starting the debugger, go to Debug / Exceptions, and select for which exceptions the debugger should stop when the exception is thrown.

peterchen
My stupidity, just came across that myself. Too bad that the debugger does not stop inside the catch scope
arionik