Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Short answer:</strong><br> Neither</p> <p><strong>Longer answer:</strong><br> using Argument*Exception (except in a library that is a product on its on, such as component library) is a smell. Exceptions are to handle exceptional situation, not bugs, and not user's (i.e. API consumer) shortfalls.</p> <p><strong>Longest answer:</strong><br> Throwing exceptions for invalid arguments is rude, unless you write a library.<br> I prefer using assertions, for two (or more) reasons: </p> <ul> <li>Assertions don't need to be tested, while throw assertions do, and test against ArgumentNullException looks ridiculous (try it). </li> <li>Assertions better communicate the intended use of the unit, and is closer to being executable documentation than a class behavior specification. </li> <li>You can change behavior of assertion violation. For example in debug compilation a message box is fine, so that your QA will hit you with it right away (you also get your IDE breaking on the line where it happens), while in unit test you can indicate assertion failure as a test failure.</li> </ul> <p>Here is what handling of null exception looks like (being sarcastic, obviously):</p> <pre><code>try { library.Method(null); } catch (ArgumentNullException e) { // retry with real argument this time library.Method(realArgument); } </code></pre> <p>Exceptions shall be used when situation is expected but exceptional (things happen that are outside of consumer's control, such as IO failure). Argument*Exception is an indication of a bug and shall be (my opinion) handled with tests and assisted with Debug.Assert</p> <p>BTW: In this particular case, you could have used Month type, instead of int. C# falls short when it comes to type safety (Aspect# rulez!) but sometimes you can prevent (or catch at compile time) those bugs all together.</p> <p>And yes, MicroSoft is wrong about that.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload