assert(0) behavior

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Mon Aug 3 18:11:31 PDT 2015


On 8/3/2015 4:57 PM, Steven Schveighoffer wrote:
> OK, this brings up another debate. The thing that triggered all this is an issue
> with core.time, see issue https://issues.dlang.org/show_bug.cgi?id=14863
>
> Essentially, we wrote code to get all the clock information at startup on a
> posix system that supports clock_gettime, which is immutable and can be read
> easily at startup. However, how we handled the case where a clock could not be
> fetched is to assert(0, "don't support clock " ~ clockname).
>
> The clear expectation was that the message will be printed (along with file/line
> number), and we can go fix the issue.
>
> On Linux 2.6.32 - a supported LTS release I guess - one of these clocks is not
> supported. This means running a simple "hello world" program crashes at startup
> in a segfault, not a nice informative message.
>
> So what is the right answer here?

The answer is the code is misusing asserts to check for environmental errors. I 
cannot understand how I consistently fail at explaining this. There have been 
many thousand message threads on exactly this topic.

Asserts are for CHECKING FOR PROGRAM BUGS.

enforce(), etc., are for CHECKING FOR INPUT ERRORS. Environmental errors are 
input errors, not programming bugs.



More information about the Digitalmars-d mailing list