What is the best pattern for handling/throwing unmanaged and CLI/CPP exceptions to .Net managed clients?

Hi:

I've got a series of C++ unmanaged classes which I have wrapped in CLI/C++ classes.
The CLI/C++ classes are consumed by a .Net (C#) application.

We have our own exception defined in the unmanaged C++ layer.
The exception class is defined as follows:

The Header Definition:
class MyException : public std::exception
{
private:
	std::string mMyExceptionMessage;

public:
	std::string GetExceptionMessage();

	MyException(const std::string &msg);

	MyException(const std::string &msg, std::exception cause);
};

Open in new window


The implementation:
std::string MeException::GetExceptionMessage()
{
	return mMeExceptionMessage;
}


MeException::MeException(const std::string &msg) : exception(msg.c_str())
{
	mMeExceptionMessage = msg;
}

MeException::MeException(const std::string &msg, std::exception cause) : exception(cause)
{
	mMeExceptionMessage = msg;
}

Open in new window


In the CLI/CPP layer, I would like catch these exceptions and rethrow them to be consumed by the .Net client.

I think I want create a CLI/CPP ref class for the exceptions for sake of uniformity.
Additionally, we want to catch std exceptions, wrap and throw as well.

Can someone point me out to the best pattern to use to implement this?

Thanks,
JohnB
LVL 1
jxbmaSoftware ConsultantAsked:
Who is Participating?
 
ambienceCommented:
Have a look at this:

https://msdn.microsoft.com/en-us/library/ms404228.aspx

// Class library written in C++/CLR.
   ThrowNonCLS.Class1 myClass = new ThrowNonCLS.Class1();

   try
   {
    // throws gcnew System::String(
    // "I do not derive from System.Exception!");
    myClass.TestThrow(); 
   }
   

   catch (Exception e)
   {
    RuntimeWrappedException rwe = e as RuntimeWrappedException;
    if (rwe != null)    
    {
      String s = rwe.WrappedException as String;
      if (s != null)
      {
        Console.WriteLine(s);
      }
    }
    else
    {
       // Handle other System.Exception types.
    }
   }           

Open in new window

0
 
jkrCommented:
The best pattern is to do - nothing. The Framework will implicitly handle that for you, see https://msdn.microsoft.com/en-us/library/aa712787%28v=vs.71%29.aspx ("14.5 Catching Unmanaged C++ Exceptions"):

When an unmanaged C++ object of type U is thrown, it is "wrapped" with a common language runtime exception of type

System::Runtime::InteropServices::SEHException
0
 
jxbmaSoftware ConsultantAuthor Commented:
ambience & jkr::> I'm still not quite sure that I follow the pattern.

Here is a code block which I'm planning on using for exception handling/processing:
try
{
	pAnUnManagedObject->DoSomething();
}
catch (System::Runtime::InteropServices::SEHException^ ex)
{
	// Do some handling here
}
catch (std::exception exception)
{
	// Do some handling here}
catch (Exception^ ex)
{
	// Do some handling here
}
catch (...)
{
	// Do some handling here
}

Open in new window


In the above call ->DoSomething(), I force a SystemAccessViolation in unmanaged code by explicitly referencing a null pointer.

When that occurs, none of my above catch blocks catch the exception.
What am I missing/doing wrong here?

Thanks,
JohnB
0
 
jkrCommented:
NULL pointer use leads to the infamous 0xc0000005, whic his a SEH exception and not a C++ exception - if you want to handle it in native C++ code, you'd have to use a

__try {

} __except(1) {

}

Open in new window


construct. Now, the bad news is that this won't work with most C++ code (the error messag roughly is 'requires unwind semantics' and I can't remember the exact error code from the top of my head). But the bottom line is not to bother and have that one cought in .NET as a SEHException.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.