Unmanaged exception caught how in managed code?

B

Bret Pehrson

Suppose the following:

// Unmanaged code
class UnmanagedException /* not visible outside of unmanaged code */
{
};
void DoSomething() /* visible (exported) to managed code */
{
throw new UnmangedException();
}

// Managed code
// Call into unmanaged code
try
{
DoSomething();
}
catch (???)
{
}


What is/are suitable types to catch in the managed code?

I see SEHException and its parent ExternalException, but these (automatic)
wrappers seem to be based on COM hresult types of exceptions.

My understanding is that the managed framework automatically catches,
repackages, and rethrows the unmanaged exception, but as what?

Thanks
 
Y

Yan-Hong Huang[MSFT]

Hello Bret,

Thanks for posting in the group.

I noticed that you posted the same question in dotnet.languages.vc group
and have replied you there. If you have time, please check my reply there.
If there is anything unclear, please feel free to post in that group and we
will follow up you there.

Have a good day.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Bret,
You can catch unmanaged exception in c# but you can't use them. you can only
rethrow them

if you have try/catch block like this
try
{
....
}
catch
{
...
throw;
}

the catch block will catch all exceptions (managed and unmanaged) and the
*thow* operator will rethrow catched exception *AS IS*.

This is the only way you can catch unmanaged exception with try/catch block
in c# and your choices are either to swallow it or rethrow it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top