Assert and the optional Message

Jonathan M Davis jmdavisProg at gmx.com
Thu Mar 8 23:54:22 PST 2012


On Friday, March 09, 2012 08:42:25 Chris Pons wrote:
> I am new to D and have been playing around with Assert. I figured
> that using a message with assert would be very helpful, but
> unfortunately when the message is printed to the console, the
> console closes and my program ends.
> 
> Is there any way to get a more permanent message?
> 
> I've tried adding system("PAUSE") and it doesn't have any effect.
> 
> Is there any way I could have the assert function print the
> message to a file?
> 
> I'm using VS2010 and Visual D if that is of any importance.

Assertions throw an AssertError when they fail. No statements after the 
assertion failure will be run. Rather, the AssertError will unwind the stack 
until it exits main. When it exits main, it gets caught, and it's printed out. 
Whatever message you gave assert it is part of that. It has nothing to do with 
files.

If you want to specifically write to a file, then you're going to have to check 
with your own function, write to the file if it fails, and then assert 
afterwards. And I'd argue that that is horribly ugly and should be avoided. 
Screwing with your program like that just because your dev environment is 
annoying enough to close its console window when the program exits isn't a 
good idea.

What you should almost certainly be doing is changing Visual Studio's settings 
so that it doesn't close the Window when the program exits. I know that it's 
possible. I've done it, but I rarely use Windows, and I don't remember what 
you have to do. So, unfortunately, I can't be of much help to you there, but 
that's the solution that you should almost certainly be using. If no one else 
around here can post how to do it, then I would think that you'd be able to 
find how via google easily enough.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list