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?
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:
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?
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With