Trace.Assert not working...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • scsharma

    #1

    Trace.Assert not working...

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

    Trace.Assert(ar gs.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 defaultevenlist ner 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
    --
    Thanks
    SCSharma
  • Jon Skeet [C# MVP]

    #2
    Re: Trace.Assert not working...

    scsharma <sharmasu@noema il.nospam> wrote:[color=blue]
    > I am having trouble with following piece of code:
    >
    > Trace.Assert(ar gs.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 defaultevenlist ner 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?[/color]

    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 IllegalArgument Exception
    ("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.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • scsharma

      #3
      Re: Trace.Assert not working...

      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.
      --
      Thanks
      SCSharma


      "Jon Skeet [C# MVP]" wrote:
      [color=blue]
      > scsharma <sharmasu@noema il.nospam> wrote:[color=green]
      > > I am having trouble with following piece of code:
      > >
      > > Trace.Assert(ar gs.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 defaultevenlist ner 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?[/color]
      >
      > 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 IllegalArgument Exception
      > ("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.
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too
      >[/color]

      Comment

      Working...