Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I like to use: <code>ArgumentException</code>, <code>ArgumentNullException</code>, and <code>ArgumentOutOfRangeException</code>.</p> <ul> <li><a href="http://msdn.microsoft.com/en-us/library/system.argumentexception(loband).aspx" rel="noreferrer"><code>ArgumentException</code></a> – Something is wrong with the argument.</li> <li><a href="http://msdn.microsoft.com/en-us/library/system.argumentnullexception(loband).aspx" rel="noreferrer"><code>ArgumentNullException</code></a> – Argument is null.</li> <li><a href="http://msdn.microsoft.com/en-us/library/system.argumentoutofrangeexception(loband).aspx" rel="noreferrer"><code>ArgumentOutOfRangeException</code></a> – I don’t use this one much, but a common use is indexing into a collection, and giving an index which is to large.</li> </ul> <p>There are other options, too, that do not focus so much on the argument itself, but rather judge the call as a whole:</p> <ul> <li><a href="http://msdn.microsoft.com/en-us/library/system.invalidoperationexception(loband).aspx" rel="noreferrer"><code>InvalidOperationException</code></a> – The argument might be OK, but not in the current state of the object. <sup><sub>Credit goes to STW (previously Yoooder). Vote <a href="https://stackoverflow.com/a/774149">his answer</a> up as well.</sub></sup></li> <li><a href="http://msdn.microsoft.com/en-us/library/system.notsupportedexception(loband).aspx" rel="noreferrer"><code>NotSupportedException</code></a> – The arguments passed in are valid, but just not supported in this implementation. Imagine an FTP client, and you pass a command in that the client doesn’t support.</li> </ul> <p>The trick is to throw the exception that best expresses why the method cannot be called the way it is. Ideally, the exception should be detailed about what went wrong, why it is wrong, and how to fix it.</p> <p>I love when error messages point to help, documentation, or other resources. For example, Microsoft did a good first step with their KB articles, e.g. <a href="http://support.microsoft.com/kb/927917" rel="noreferrer">“Why do I receive an "Operation aborted" error message when I visit a Web page in Internet Explorer?”</a>. When you encounter the error, they point you to the KB article in the error message. What they don’t do well is that they don’t tell you, why specifically it failed.</p> <p><sup><sub>Thanks to STW (ex Yoooder) again for the comments.</sub></sup></p> <hr> <p>In response to your followup, I would throw an <a href="http://msdn.microsoft.com/en-us/library/system.argumentoutofrangeexception(loband).aspx" rel="noreferrer"><code>ArgumentOutOfRangeException</code></a>. Look at what MSDN says about this exception:</p> <blockquote> <p><code>ArgumentOutOfRangeException</code> is thrown when a method is invoked and at least one of the arguments passed to the method is not null reference (<code>Nothing</code> in Visual Basic) and does not contain a valid value.</p> </blockquote> <p>So, in this case, you are passing a value, but that is not a valid value, since your range is 1–12. However, the way you document it makes it clear, what your API throws. Because although I might say <code>ArgumentOutOfRangeException</code>, another developer might say <code>ArgumentException</code>. Make it easy and document the behavior.</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