Trace.Assert not working...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I am having trouble with following piece of code:

Trace.Assert(args.Length >=1,"Invalid Argument list. Statement Date is
mandatory input");

I expect the code to stop execution and exit when args.length >= 1
condition is not true. I am using C#.
But whats happening is that i see dialog box when this condition is not
true even if i am in release mode. If i remove defaultevenlistner then code
does not show the daialog box but at same time it does code does not exit.
Code continues past the assert statement. What am i missing?
Thanks
 
scsharma said:
I am having trouble with following piece of code:

Trace.Assert(args.Length >=1,"Invalid Argument list. Statement Date is
mandatory input");

I expect the code to stop execution and exit when args.length >= 1
condition is not true. I am using C#.
But whats happening is that i see dialog box when this condition is not
true even if i am in release mode. If i remove defaultevenlistner then code
does not show the daialog box but at same time it does code does not exit.
Code continues past the assert statement. What am i missing?

No, Trace.Assert isn't guaranteed to throw exceptions - it depends on
what trace listeners are attached to it.

If you really want an exception to be thrown (which seems reasonably),
you should just do:

if (args.Length < 1)
{
throw new IllegalArgumentException
("Invalid Argument list. Statement Date is mandatory input");
}

Of course, you could always add your own TraceListener which throws an
exception, but then you won't get the same behaviour if TRACE isn't
defined.
 
Interesting, I am not sure where but somewhere i had read that when assertion
fails the program execution is stopped but after reading Jon's response i
checked C# documentation and could not verify my assumption. That forces me
to think why would i ever use assert statements(in release mode) if the
program is not going to stop excution when assertions fail.It would make
sense for code to exit when assertions failed.
 
Back
Top