Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug.Fail and Debug.Assert

Tags:

.net

debugging

I have a question about the Debug.Fail method which puzzles me. It's from a MeassureUp test and goes like this:

To improve the reliability of your code, you want to verify an important value and stop execution if the value is not set properly. However, you want to stop execution only during debugging. You do not want users with release versions of your application to experience problems.

Which method should you use?

  • Debug.Assert (my answer)
  • Debug.Flush
  • Debug.Fail (correct answer according to MeassureUp test)
  • Debug.Indent

I answered Debug.Assert because it has a boolean condition as a parameter, which means it can verify a value. Debug.Fail only has strings as parameters for whatever message should be output. But here is the official reasoning:

  • Debug.Fail causes the debugger to break at the line of code and output a failure message.
  • Debug.Assert evaluates a condition and displays a message, but it does not interrupt processing.
  • Debug.Flush flushes the debug output buffer.
  • Debug.Indent controls the output formatting.

What do they mean by that the Debug.Assert "does not interrupt processing"? Both Debug.Assert and Debug.Fail gives a similar popup windows with Abort, Retry and Ignore buttons. Furthermore, Debug.Assert evaluates an expression.

Is the official answer wrong including their reasoning or am I misunderstanding something?

like image 993
Kasper Hansen Avatar asked May 25 '13 13:05

Kasper Hansen


2 Answers

There's just no difference. Debug.Assert() calls Fail() when the condition is false. So there's no difference either between Assert() and an if-statement that calls Fail().

I'd suggest you find a better web site.

like image 74
Hans Passant Avatar answered Oct 14 '22 09:10

Hans Passant


I use Debug.Fail() typically in switch/default: statements where default: should never occur (in addition to throwing an exception or whatever other error handling)

like image 45
Serge Wautier Avatar answered Oct 14 '22 11:10

Serge Wautier