assert(0)

Idan Arye via Digitalmars-d digitalmars-d at puremagic.com
Sun Nov 8 02:22:29 PST 2015


On Saturday, 7 November 2015 at 21:24:02 UTC, Fyodor Ustinov 
wrote:
> Colleagues, IMHO:
>
> If "assert" catch "fundamental programmers errors" - it should 
> hang programm immediately with and without "-release".
>
> If "assert" catch not "too fundamental" errors - assert(0) 
> should emit "Error" in both cases.
>
> Third option - assert(0) - it's a "special case" and halt 
> program in both cases.
>
> But there should't be so that in one case the "fundamental 
> error" and the other "not fundamental".
>
> It's my opinion.
>
> WBR,
>     Fyodor.

I strongly disagree. Without `-release`, the job of "fundamental 
programmers errors" is not to stop the program and prevent 
farther corruption(which is pointless - how do you know executing 
the `scope(exit)` and `scope(failure)` blocks will increase the 
corruption? Maybe they'll reduce it?), because when you are 
developing you shouldn't work with the only copies of important 
data files. Without `-release`, the role of `assert`s(and 
`Error`s in general) is the help the programmer fix these bugs.

When I have a "fundamental programmer error" in my code, I prefer 
to get a stack trace from the exception mechanism than to find a 
core dump(that was hopefully generated) and rely on GDB's 
excellent support for D to analyze it.

Besides, there are some very specific cases where it's acceptable 
to catch `Error`s - one of them is when you have a logging 
mechanism that can log these errors in a way/place that's easier 
for you to read - and then of course re-throws them. Halting the 
program on errors prevents this logging mechanism from doing it's 
thing.


More information about the Digitalmars-d mailing list