Jump to page: 1 25  
Page
Thread overview
Assert and the optional Message
Mar 09, 2012
Chris Pons
Mar 09, 2012
Jonathan M Davis
Mar 09, 2012
Chris Pons
Mar 09, 2012
Chris Pons
Mar 09, 2012
Jonathan M Davis
Mar 09, 2012
Regan Heath
Mar 09, 2012
Jonathan M Davis
Mar 09, 2012
Regan Heath
Mar 09, 2012
Jacob Carlborg
Mar 09, 2012
Timon Gehr
Mar 09, 2012
Jacob Carlborg
Mar 09, 2012
Timon Gehr
Mar 09, 2012
Jonathan M Davis
Mar 09, 2012
Timon Gehr
Mar 09, 2012
H. S. Teoh
Mar 09, 2012
Timon Gehr
Mar 09, 2012
Jonathan M Davis
Mar 10, 2012
Jacob Carlborg
Mar 10, 2012
Jonathan M Davis
Mar 11, 2012
Jacob Carlborg
Mar 11, 2012
Jonathan M Davis
Mar 12, 2012
Jacob Carlborg
Mar 12, 2012
Jonathan M Davis
Mar 12, 2012
Jacob Carlborg
Mar 12, 2012
Jacob Carlborg
Mar 10, 2012
Jacob Carlborg
Mar 10, 2012
Jacob Carlborg
Mar 10, 2012
Jonathan M Davis
Mar 11, 2012
Jacob Carlborg
Mar 09, 2012
H. S. Teoh
Mar 10, 2012
Jacob Carlborg
Mar 10, 2012
Jonathan M Davis
Mar 11, 2012
Jacob Carlborg
Mar 09, 2012
Mars
Mar 09, 2012
Jonathan M Davis
Mar 09, 2012
Ali Çehreli
Mar 09, 2012
Mike Parker
Mar 09, 2012
Dmitry Olshansky
Mar 09, 2012
Chris Pons
Mar 09, 2012
Jonathan M Davis
Mar 09, 2012
H. S. Teoh
Mar 09, 2012
Timon Gehr
Mar 09, 2012
Jonathan M Davis
Mar 09, 2012
Timon Gehr
Mar 10, 2012
Jonathan M Davis
Mar 09, 2012
Jonathan M Davis
Mar 09, 2012
H. S. Teoh
March 09, 2012
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.

March 09, 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
March 09, 2012
Ok, thank you for the advice. I will look into it.
March 09, 2012
Any idea why, system("PAUSE") does work?
March 09, 2012
On Friday, March 09, 2012 09:20:33 Chris Pons wrote:
> Any idea why, system("PAUSE") does work?

Because that line never gets run. A failed assertion throws an AssertError which skips everything until it gets handled by the runtime after it exits main. So, if you have

assert(condition);
system("PAUSE");

the second line will never execute as long as condition is false.

- Jonathan M Davis
March 09, 2012
On Friday, 9 March 2012 at 07:55:56 UTC, Jonathan M Davis wrote:
> 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

Are you sure you've tested this with D? Because VisualD doesn't support that yet, afaik.
March 09, 2012
On Fri, 09 Mar 2012 08:20:33 -0000, Chris Pons <cmpons@gmail.com> wrote:

> Any idea why, system("PAUSE") does work?

As Jonathan says, assert throws AssertError... which means, if you wanted to you could catch it, for example...

module asserting;

import std.stdio;
import core.exception;

debug
{
    import std.c.windows.windows;
    extern (C) int kbhit();
}

void main()
{
    try
    {
        assert(false, "testing assert");
    }
    catch(AssertError e)
    {
        debug
        {
            writefln("Assertion failed: %s", e);
            writefln("Press any key to exit...");
            while(!kbhit())
                Sleep(1);
        }
        throw e;
    }
}

So, for debug builds your exe will output the assertion manually, and wait for a key press.  Then re-throw the assertion (resulting in the d runtime outputting the assertion again).  In release, no pause.

Note: kbhit and Sleep are windows specific (sleep would be the unix equivalent, not sure on one for kbhit).

Regan Heath

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/
March 09, 2012
On Friday, March 09, 2012 10:55:10 Mars wrote:
> On Friday, 9 March 2012 at 07:55:56 UTC, Jonathan M Davis wrote:
> > 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
> 
> Are you sure you've tested this with D? Because VisualD doesn't support that yet, afaik.

I've never used Visual Studio with D, and I wouldn't expect telling it not close the window after execution to be any different in D than it is in C++, but I don't know. Regardless, contorting your program to try and print assert messages to a file just because VisualD has problems with closing the Window on you is a _bad_ idea IMHO. If it really has that problem, and you really need to see the assertion without running the debugger on it, I'd advise just running the program on the command line.

- Jonathan M Davis
March 09, 2012
On Friday, March 09, 2012 11:03:38 Regan Heath wrote:
> On Fri, 09 Mar 2012 08:20:33 -0000, Chris Pons <cmpons@gmail.com> wrote:
> > Any idea why, system("PAUSE") does work?
> 
> As Jonathan says, assert throws AssertError... which means, if you wanted to you could catch it, for example...
> 
> module asserting;
> 
> import std.stdio;
> import core.exception;
> 
> debug
> {
>      import std.c.windows.windows;
>      extern (C) int kbhit();
> }
> 
> void main()
> {
>      try
>      {
>          assert(false, "testing assert");
>      }
>      catch(AssertError e)
>      {
>          debug
>          {
>              writefln("Assertion failed: %s", e);
>              writefln("Press any key to exit...");
>              while(!kbhit())
>                  Sleep(1);
>          }
>          throw e;
>      }
> }
> 
> So, for debug builds your exe will output the assertion manually, and wait for a key press.  Then re-throw the assertion (resulting in the d runtime outputting the assertion again).  In release, no pause.
> 
> Note: kbhit and Sleep are windows specific (sleep would be the unix equivalent, not sure on one for kbhit).

Bet advised that catching anything not derived from Exception is generally a _bad_ idea (most of which are derived from Error), and AssertError is _not_ derived from Exception but rather Error. AssertErrors are not guaranteed to invoke finally blocks, scope statements, or destructors. Your program is in an invalid state when you catch an Error. So, while it may work in this particular case, it is _not_ something that you should be doing in general. By far the worst thing to do would be to catch an Error and then continue executing. Errors are _supposed_ to kill your program.

- Jonathan M Davis
March 09, 2012
On 3/9/2012 4:42 PM, 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.
>

In this case, you can just open up a command window, navigate to your executable directory, and execute the app manually from the command line. Then you don't have to worry about it closing.

Of course, this doesn't work if you are using the default output directories for your executable, but you have resources in the working directory. When I use VisualD, I generally configure my executables to all go a single bin directory (foo.exe for release, foo_dbg.exe for Debug). Then I keep a command window open in the background. That way, if I need to run from the command line, it's easy to do so without any copying around.
« First   ‹ Prev
1 2 3 4 5