How to map/marshall native exceptions into managed exceptions?
Posted on 2009-05-01
I am creating a DLL that contains a native "C++" class "CR", and a managed wrapper class "CM" that wraps around CR via a pointer.
CR defines and uses "C++" native exceptions.
In CM, I have created managed versions of the unmanaged exceptions in CR.
The methods in CM that invoke, via CR's pointer, methods in the unmanaged code of CR have try...catch...finally. For each possible native exception that could be thrown through from the unmanaged method, I have placed a catch. Inside the catch, I simply throw the managed version of the native exception. Basically, this is how I am providing the mapping from unmanaged to managed exceptions.
If I do not catch an unmanaged exception, I expect that an SEH exception will be generated.
From what I have read, this process should work, but whenever I force my native code to throw an unmanaged exception, what I catch in my CM wrapper class is an SEH exception. Arggg... So much for me understanding things! :P
So, I am hoping that someone can tell me the best practices way to marshall exceptions between managed and unmanaged code... Must I replace my native "C++" code with SEH (hope not)? Is there an interop method that provides mapping that I must override? Should I just define managed exceptions in my unmanaged code (i would prefer to keep the worlds separate, if possible)?
I would sincerely appreciate you help with these questions...