HighTechTalks DotNet Forums  

When is Debug.Assert pertinent to use ?

Dotnet Framework microsoft.public.dotnet.framework


Discuss When is Debug.Assert pertinent to use ? in the Dotnet Framework forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Steve B.
 
Posts: n/a

Default When is Debug.Assert pertinent to use ? - 06-21-2006 , 08:42 AM






Hi,

I'm wondering when does Debug.Assert is pertinent to use.

For the moment, I think it is used to ensure something is true, as it is
supposed to be always true.

For example, I query a DB to get the only one customer's detail rows.
If I get any exception, it is an runtime error..So I use try/catch/throw
blocks.
If I get 0 rows, it is a business error, so I throw a business exception.
If I get 1 row, it's ok and I return it.
If I get 2 or more row, it's a supposed-impossible case, but I use
Debug.Assert to check it actually does not occurs. If it occurs, it should
help me debug the application.

Am I on the right way ?

Steve



Reply With Quote
  #2  
Old   
Markus Kling
 
Posts: n/a

Default Re: When is Debug.Assert pertinent to use ? - 06-21-2006 , 11:51 AM






Steve,

you should use error handling code (exceptions) for conditions you expect to
occur and assertions for conditions that should never occur.

In other words, the general strategy is to use asserts everywhere where
something must to be true and it not being true would be a programming error
of some type. Exceptions should be used where it is an error condition that
could happen in a real system but is relatively unusual (like your method
not returning a record).

Imho your approach is right.
Markus


"Steve B." <steve_beauge (AT) com (DOT) msn_swap_com_and_msn> schrieb im Newsbeitrag
news:O5PTtATlGHA.3816 (AT) TK2MSFTNGP02 (DOT) phx.gbl...
Quote:
Hi,

I'm wondering when does Debug.Assert is pertinent to use.

For the moment, I think it is used to ensure something is true, as it is
supposed to be always true.

For example, I query a DB to get the only one customer's detail rows.
If I get any exception, it is an runtime error..So I use try/catch/throw
blocks.
If I get 0 rows, it is a business error, so I throw a business exception.
If I get 1 row, it's ok and I return it.
If I get 2 or more row, it's a supposed-impossible case, but I use
Debug.Assert to check it actually does not occurs. If it occurs, it should
help me debug the application.

Am I on the right way ?

Steve




Reply With Quote
  #3  
Old   
David Browne
 
Posts: n/a

Default Re: When is Debug.Assert pertinent to use ? - 06-21-2006 , 01:55 PM




"Steve B." <steve_beauge (AT) com (DOT) msn_swap_com_and_msn> wrote

Quote:
Hi,

I'm wondering when does Debug.Assert is pertinent to use.

For the moment, I think it is used to ensure something is true, as it is
supposed to be always true.

For example, I query a DB to get the only one customer's detail rows.
If I get any exception, it is an runtime error..So I use try/catch/throw
blocks.
If I get 0 rows, it is a business error, so I throw a business exception.
If I get 1 row, it's ok and I return it.
If I get 2 or more row, it's a supposed-impossible case, but I use
Debug.Assert to check it actually does not occurs. If it occurs, it should
help me debug the application.

Am I on the right way ?

No. Debug.Assert does not affect the control flow and is not triggered in
release builds. All of the Debug. methods are generally not very usefull.
Use System.Diagnostics.Trace to output trace information, and use Exceptions
to indicate program failures.

You should throw an Exception (perhaps an InvalidOperationException), when
something bad happens. This will stop code execution both in debug and
release builds, and give you the stack trace information to debug it.

David



Reply With Quote
  #4  
Old   
Jim Wooley
 
Posts: n/a

Default Re: When is Debug.Assert pertinent to use ? - 06-21-2006 , 03:54 PM



Also, remember that the Debug.Assert portions are for developer time issues
only. They are excluded on release build so they won't be fired when one
of the situations outside the expected hits on a deployed app.

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx

Quote:
Steve,

you should use error handling code (exceptions) for conditions you
expect to occur and assertions for conditions that should never occur.

In other words, the general strategy is to use asserts everywhere
where something must to be true and it not being true would be a
programming error of some type. Exceptions should be used where it is
an error condition that could happen in a real system but is
relatively unusual (like your method not returning a record).

Imho your approach is right.
Markus
"Steve B." <steve_beauge (AT) com (DOT) msn_swap_com_and_msn> schrieb im
Newsbeitrag news:O5PTtATlGHA.3816 (AT) TK2MSFTNGP02 (DOT) phx.gbl...

Hi,

I'm wondering when does Debug.Assert is pertinent to use.

For the moment, I think it is used to ensure something is true, as it
is supposed to be always true.

For example, I query a DB to get the only one customer's detail rows.
If I get any exception, it is an runtime error..So I use
try/catch/throw
blocks.
If I get 0 rows, it is a business error, so I throw a business
exception.
If I get 1 row, it's ok and I return it.
If I get 2 or more row, it's a supposed-impossible case, but I use
Debug.Assert to check it actually does not occurs. If it occurs, it
should
help me debug the application.
Am I on the right way ?

Steve




Reply With Quote
  #5  
Old   
Christoph Richter [AT]
 
Posts: n/a

Default Re: When is Debug.Assert pertinent to use ? - 06-21-2006 , 07:07 PM



the real benefit is, that with debug.assert you have also an stop
output window. where i use it most times is, that theres an
specification, that the pages of the aspnet site, for excample are not
allowed to exceed 40kb. so i make an function, that check the size of
the output page, and debug.assert that its smaller.... so i can
develop and if i exceed it, i'll know it right away....

so stuff, thats not directly true false, but for warnings, when you
exceed some parameters....

cheers,
christoph


On Wed, 21 Jun 2006 14:42:35 +0200, "Steve B."
<steve_beauge (AT) com (DOT) msn_swap_com_and_msn> wrote:

Quote:
Hi,

I'm wondering when does Debug.Assert is pertinent to use.

For the moment, I think it is used to ensure something is true, as it is
supposed to be always true.

For example, I query a DB to get the only one customer's detail rows.
If I get any exception, it is an runtime error..So I use try/catch/throw
blocks.
If I get 0 rows, it is a business error, so I throw a business exception.
If I get 1 row, it's ok and I return it.
If I get 2 or more row, it's a supposed-impossible case, but I use
Debug.Assert to check it actually does not occurs. If it occurs, it should
help me debug the application.

Am I on the right way ?

Steve

Cheers,
Christoph

Vienna/Austria

www.code4ward.net/blogs/cmn


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.