Assert and the optional Message

Steven Schveighoffer schveiguy at yahoo.com
Mon Mar 12 07:20:54 PDT 2012


On Sat, 10 Mar 2012 01:09:02 -0500, Jonathan M Davis <jmdavisProg at gmx.com>  
wrote:

> On Friday, March 09, 2012 22:48:25 Timon Gehr wrote:
>> On 03/09/2012 10:43 PM, Jonathan M Davis wrote:
>> ...
>>
>> >> Jonathan is just speculating. And I think he is wrong.
>> >
>> > Speculating about what?
>> >
>> > - Jonathan M Davis
>>
>> About how assertion failures affect struct destructors, finally
>> statements and scope statements. If they wouldn't be executed upon
>> thrown AssertError, then a _passing_ in contract could screw up the
>> program state. Furthermore, the implementation does execute them.
>
> It's not speculation. There's no guessing involved. It's what I've  
> understood
> the rules of the language to be.
>
> But I asked on dmd-internals to verify it, and Walter has stated that it  
> just
> so happens that  the destructors, scope statements, and finally blocks do
> currently get run for non-Exception exceptions, but it's not guaranteed:
>
> http://forum.dlang.org/post/4F5AE8EC.4040408@digitalmars.com

I understood this to mean, in release mode, assert(0) may simply abort  
without being catchable.

If assert isn't going to properly unwind the stack, then we are using the  
wrong tool for unit tests.  Asserts during unit tests do not protect some  
valid program state, they are simply a means to test that a function is  
operating as designed.  A unit test should test one thing, maybe repeated  
times, and then exit.  It should not save any state or affect any other  
unit tests or the program.

If there is ever an effort to prevent assert from running finally/scope  
blocks, special effort should be take to continue running those blocks  
during unit tests.

-Steve


More information about the Digitalmars-d-learn mailing list