简体   繁体   English

在dotnet核心中捕获本地异常

[英]catch native exception in dotnet core

in a dotnet core application that uses pinvoke and runs on linux, when c++ throws - for example - std::runtime_error - we get: 在使用pinvoke并在linux上运行的dotnet核心应用程序中,当c ++抛出时-例如std::runtime_error我们得到:

terminate called recursively
terminate called after throwing an instance of 'terminate called after throwing an instance of 'std::runtime_error*'
Aborted (core dumped)

even though the invocation of the externed c++ method is wrapped in a try catch block in the managed code. 即使外部c ++方法的调用包装在托管代码的try catch块中。

how can this be caught and treated in the dotnet core managed code? 如何在dotnet核心托管代码中捕获和处理此问题?

I set-up this Minimal, Complete and Verifiable example that demonstrates how native exceptions are NOT caught whatsoever by managed C# .NET Core code, on Linux. 我设置了这个最小,完整和可验证的示例 ,该示例演示了在Linux上托管C#.NET Core代码如何不捕获任何本地异常。

As described in the issue I opened for dotnet/coreclr , I've tried (m)any possible weapon(s) in the arsenal, to no avail. 我为dotnet/coreclr打开的问题所述 ,我已经尝试过在军械库中使用任何可能的武器,但均无济于事。

The direct answer given by dotnet team was: dotnet团队给出的直接答案是:

We do not support exception handling interop on Unix. 我们不支持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 . Mono项目在这里有很棒的文章: http : //www.mono-project.com/docs/advanced/pinvoke/#runtime-exception-propagation The same reasoning applies to .NET Core. 相同的推理适用于.NET Core。

The Mono project's solution, which is also recommended by the dotnet team, is: dotnet团队还建议Mono项目的解决方案是:

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. C ++异常将需要映射到“ out”参数或返回值中,以便托管代码可以知道发生了什么错误,并(可选)抛出托管异常以“传播”原始C ++异常。

That's what we eneded up implementing and, well, it works :). 这就是我们加强的实现,而且,它很有效:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM