P/Invoke Exceptions

Chapter 7, “Leveraging Existing Code—P/Invoke,” dealt with calls into unmanaged code using P/Invoke. Next, you will look specifically at what happens to exceptions that are generated from the unmanaged code.

If an exception is generated in unmanaged code, it is caught and translated to an appropriate managed exception and rethrown. The only exception would be when no appropriate managed exception exists to which to convert. You have already seen that throwing an unspecified exception such as the following

Throw;

results in an SEHException. In fact, any exception that is thrown that cannot be translated to its managed counterpart results in an SEHException.

A sample in the UnmanagedException directory raises four different ...

Get .NET Common Language Runtime Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.