Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to catch in managed code any user exception thrown from native Linux code #10794

Closed
yaireclipse opened this issue Jul 30, 2018 · 6 comments
Labels
area-Interop-coreclr question Answer questions and provide assistance, not an issue with source code or documentation.

Comments

@yaireclipse
Copy link

Basically, I have a solution that contains one project of managed C# .NET Core and another native linux C++ project. The managed code project uses the native one. In the native code I throw various C++ exceptions (such as runtime_error). In the managed code, I call the native code within a try-catch block, expecting to catch the offending exception(s) in the managed code.

I've tried:

  1. catch (Exception);
  2. catch (System.Runtime.CompilerServices.RuntimeWrappedException);
  3. Parameterless catch {..};
  4. Using [SecurityCritical] + [HandleProcessCorruptedStateExceptions], although I thought these exceptions are probably not considered corrupted-state exception, and even if they did, in Strip corrupted state exceptions handling #7272 @jkotas states that corrupted-state exceptions are not converted to managed exceptions;
  5. Setting legacyCorruptedState­­ExceptionsPolicy to true (again - although I thought I'm not dealing with corrupted-state exceptions).

All, to no avail.

I've set-up this Minimal, Complete and Verifiable example that demonstrates what I currently fail to achieve, and the different attempts to solve it.

Tested with .NET Core 2.0, on Ubuntu 16.04.2 LTS (via Bash on Ubuntu on Windows) and CentOS Linux release 7.5.1804 (via Docker for Windows).

This was actually asked in the past on Stackoverflow by @levhaikin, a colleage of mine, with no answers, so we turn to you :) :

  1. Is my expectation correct? Meaning, is there a way, in .NET Core, to catch an exception thrown from native code, in managed code?
  2. If so, what am I doing wrong? How should it be done?

Thanks,
Yair

@jkotas
Copy link
Member

jkotas commented Jul 30, 2018

We do not support exception handling interop on Unix. There is no good way to do it. The Mono project has a great write up on it here: http://www.mono-project.com/docs/advanced/pinvoke/#runtime-exception-propagation . The same reasoning applies to .NET Core.

@yaireclipse
Copy link
Author

Wow. Good reading indeed!

In the end of the referenced section in that article, the author recommends as follows:

C++ exceptions will need to be mapped into an “out” parameter or a return value, so that managed code can know what error occurred, and (optionally) throw a managed exception to “propagate” the original C++ exception.

@jkotas, do you recommend the same approach for .NET Core wrapping native C++ as well?

Thanks for the quick response and the reference!

@jkotas
Copy link
Member

jkotas commented Jul 30, 2018

do you recommend the same approach for .NET Core wrapping native C++ as well?

Yes.

@yaireclipse
Copy link
Author

OK. Thank you for your time :) I'm closing this issue.

@AaronRobinsonMSFT
Copy link
Member

@yaireclipse This would be entirely possible if the following proposal was adopted: https://groups.google.com/a/isocpp.org/forum/#!msg/std-proposals/BnNRLtFXJMA/gZcutJGsBQAJ

@yaireclipse
Copy link
Author

@AaronRobinsonMSFT, thanks for the interesting reference. Sounds like a good direction for C programming in general. Also might enable automatic translation of exceptions in non-Windows native code to managed code, and maybe even unifying Windows and non-Windows behavior.

@msftgits msftgits transferred this issue from dotnet/coreclr Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Interop-coreclr question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
None yet
Development

No branches or pull requests

3 participants