Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.
How to wrap a native exception in C++/CLI

Answered How to wrap a native exception in C++/CLI

  • Thursday, March 31, 2011 4:53 AM
     
      Has Code

    Too much late-night programming... I feel like I'm missing something obvious again.

    I have a managed function calling an unmanaged function in C++/CLI. The unmanaged function throws a std::exception. The managed function attempts to catch said exception. Unfortunately, the try/catch block fails to catch the unmanaged exception and insists on throwing an SEHException instead.

     

    .

    void MyClass::MyFunc()
    {
    	try
    	{
    		_unmanaged->DoExcept();
    	} //<-- debugger shows SEHException is thrown here
    	catch(const std::exception& e)
    	{
    		//Doesn't go here
    	}
    	catch(...)
    	{
    		//Goes here, but appears to be catching SEHException...
    	}
    }
    
    

All Replies

  • Thursday, March 31, 2011 6:37 AM
     
     

    First of all check if _unmanaged contains a valid non-null value and the ‘throw std::exception’ statement is reached.

  • Thursday, March 31, 2011 6:51 AM
     
     

    _unmanaged definitely contains a valid reference. I inserted a throw new exception; as the first statement just to be sure :(

     

    I can't step into the static library, though. Is this normal or are there settings I need to tweak? 

     

    My references look kinda like this:
    WPF Application › C++ / CLI Interop › Static Library.

  • Thursday, March 31, 2011 7:02 AM
     
     Answered Has Code

    I think you should use ‘throw std::exception()’ without ‘new’, or add ‘catch(const std::exception * exc)’, but sometimes it is difficult to delete such objects.

     

    In order to check if the exception is caught, try this fragment in your managed code:

     

    try
    {
             //_unmanaged->DoExcept();
             throw std::exception();
    }
    catch(const std::exception & exc)
    {
             //. . . .
    }
    catch(...)
    {
             //. . . .
    }

     

  • Thursday, March 31, 2011 7:38 AM
    Moderator
     
     

    It's likely that you're using /clr:pure and not /clr. This means that native function calls are done through PInvoke and PInvoke replaces C++ exceptions (and all other native exceptions it doesn't recognize) with SEHException.

  • Thursday, March 31, 2011 4:28 PM
     
     

    That did it. Thanks :)

     

    Any idea why I can't step into the native code? Is this even possible? I'm getting some SEHExceptions I can't catch with std::exception blocks and I'd like to know what they are...

  • Saturday, August 11, 2012 2:26 PM
     
     
    In order to step into unmanaged code when debugging a managed application, you have to turn on a debugger setting in your startup project (Debug Unmanaged Code).