0

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.

arionik
  • 100
  • 2
  • 9
  • There's no such thing as VS2009. I've edited your question to match your tag, 2008. If you are referring to the new 2010 release, please edit your question to indicate that. – Billy ONeal Apr 22 '10 at 14:26
  • 1
    If you place a breakpoint on the line `throw hr;`, do you hit the breakpoint? – James McNellis Apr 22 '10 at 14:47
  • I do hit it, after that it gets me somewhere into disassembly saying "Microsoft C++-exception: long at memory position 0x0016c814" – arionik Apr 22 '10 at 14:55

2 Answers2

1

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
  • 104,103
  • 58
  • 317
  • 552
1

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

peterchen
  • 40,917
  • 20
  • 104
  • 186
  • My stupidity, just came across that myself. Too bad that the debugger does not stop inside the catch scope – arionik Apr 22 '10 at 16:59