-1

I am using one function from native .dll in C# code and sometimes the following assert is calling:

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

But I can not catch it in my .NET application (it's just crashing). How can I handle this?

Ivan Kochurkin
  • 4,413
  • 8
  • 45
  • 80
  • You can't. Make sure that you're calling the code correctly instead of relying on exceptions. This is exactly what asserts are for. – SBI Mar 04 '15 at 08:38
  • 2
    It's *meant* to crash/abort. The fix is to not pass a (combination of) inputs to the function such that they violate the necessary preconditions for the function, including this one. – Damien_The_Unbeliever Mar 04 '15 at 08:39
  • You need to stay a mile away from code that is so unreliable that the author doesn't trust it enough to give you the Release build. Or you'd better establish a good working relationship with him, it is going to take a while to get the bugs out. Most important thing for you to do is to give him small repro projects so he can fix his bugs. – Hans Passant Mar 04 '15 at 11:31
  • @HansPassant, thank you. I wrote to the author of code. – Ivan Kochurkin Mar 05 '15 at 08:59

1 Answers1

1

At least by reading the 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().

And the MSDN

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

so no exception... an error message and the program dies. Nothing to catch here :-)

Then we could talk about the difference in parameters handling between C and C#... In C you kill the program with an assert, in C# you throw a catcheable exception (ArgumentException)... Different methodologies :-)

Now, how to handle it? Don't cause assert to fail is a good method :-) assert are terminal errors (because even in C they can't be handled), so they shouldn't happen.

xanatos
  • 109,618
  • 12
  • 197
  • 280