• bvs23bkv33 (unregistered)

    assert(forst)

  • Harald van Dijk (unregistered)

    This doesn't look like a WTF to me, and the comment about assert(false) looks right as well. What assert(false) does in C, assuming NDEBUG is defined, is print information about the current code file and line and the condition that failed, and abort the program. It's useful for code that can logically never be reached, but where execution cannot meaningfully continue if it is reached anyway due to some bug. For example, the default: case of an exhaustive switch. That's what this code does as well. Throwing an exception is done to get a meaningful stack trace in the output. Catching it, printing it there, and aborting the program is done is as close to assert(false) as you can reasonably get, and it can be the correct behaviour if it is an internal logic error that callers must absolutely not attempt to handle.

  • (nodebb) in reply to Harald van Dijk

    In Java you can simply write:

    logger.fatal(msg, new Exception ());

    because stack trace is recorded while creating the Exception object, not while throwing it.

    Also, link to original is wrong.

  • Stephan L. (unregistered) in reply to antoshka

    Throwing the exception gives you a last chance to enter the debugger with full stack intact. I do that in many places.

    We all agree that a nice comment explaining why this works would be great...

  • djingis1 (unregistered)

    Of course, java actually has the assert keyword. And that throws an exception, with full stack trace.

  • (nodebb)

    And we won't mention that calling assert(false) isn't in C. C++, yes, but not in C. And we won't mention that it's a sub-optimal way of doing it, too.

    assert(!"Some message that gives a hint about what went wrong");

    The naked string is not NULL, therefore it is true, so the shriek in front makes the whole expression false.

  • Mike (unregistered) in reply to djingis1

    Failing an assertion in Java throws an Error, not an Exception, huge difference.

    Also, assertions are skipped by default, and turned on by a runtime flag. They're a tool for a different purpose.

  • Mike (unregistered) in reply to djingis1

    Failing an assertion in Java throws an Error, not an Exception, huge difference.

    Also, assertions are skipped by default, and turned on by a runtime flag. They're a tool for a different purpose.

  • Quite (unregistered)

    The singularity is approaching ... close, but no cigar.

  • Unhelpful (unregistered)

    What is this Quad and how does "the sis" help?

  • Link8312 (google)

    Unfortunately on some platforms assert(!"error message") gives me a "condition is constant" warning. On these platforms I use assert(strlen("error message")==0), preferably wrapped in a shorter macro.

    Addendum 2017-06-13 11:22: That was in reply to Steve_The_Cynic, I lost my "in reply to" to login issues.

  • (nodebb) in reply to Link8312

    That works, too. (And, of course, any form of visibly unconditional assert will do that. assert(0);, for example.)

  • DMLou (unregistered) in reply to Steve_The_Cynic

    Actually, as of C99, assert(false) is valid C, though you need to include stdbool.h to get it.

  • (nodebb) in reply to DMLou

    Too much code in my $JOB that doesn't use C99 features. Bah.

  • DMLou (unregistered) in reply to Steve_The_Cynic

    Yeah... I know the feeling. I've been there before... and I'm there to a similar extent now since I wish I could use C++11/14 features on my job, but RHEL doesn't ship with a version of gcc with non-experimental support for those features.

Leave a comment on “Classic WTF: It's Like Calling Assert”

Log In or post as a guest

Replying to comment #:

« Return to Article