- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
assert(forst)
Admin
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.
Admin
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.
Admin
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...
Admin
Of course, java actually has the
assert
keyword. And that throws an exception, with full stack trace.Admin
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.
Admin
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.
Admin
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.
Admin
The singularity is approaching ... close, but no cigar.
Admin
What is this Quad and how does "the sis" help?
Admin
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.
Admin
That works, too. (And, of course, any form of visibly unconditional assert will do that.
assert(0);
, for example.)Admin
Actually, as of C99, assert(false) is valid C, though you need to include stdbool.h to get it.
Admin
Too much code in my $JOB that doesn't use C99 features. Bah.
Admin
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.