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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.