无法捕获来自 .NET 中本机 dll 的断言调用

Can not catch assert call from native dll in .NET

我在 C# 代码中使用本机 .dll 中的一个函数,有时会调用以下断言:

#include <assert.h>
...
assert(e > 0.0);

但我无法在我的 .NET 应用程序中捕获它(它只是崩溃)。我该如何处理?

至少阅读 Wiki:

When executed, if the expression is false (that is, compares equal to 0), assert() writes information about the call that failed on stderr and then calls abort().

还有 MSDN

Evaluates an expression and, when the result is false, prints a diagnostic message and aborts the program.

所以没有例外...错误消息和程序终止。这里没什么可看的:-)

然后我们可以谈谈 C 和 C# 之间参数处理的区别...在 C 中,您使用 assert 终止程序,在 C# 中,您抛出可捕获的异常 (ArgumentException) ...不同的方法:-)

现在,如何处理呢?不要导致 assert 失败是一个好方法:-) assert 是终端错误(因为即使在 C 中也无法处理),所以它们不应该发生。