The .NET Framework has a lot of pre-defined exceptions. For example, if an invalid parameter was passed to my method, I’m supposed to raise an ArgumentException (or ArgumentNullException, if the paramter was null). If a method is called that is invalid for the current object state, I’m supposed to raise an InvalidOperationException.
Is there some pre-defined exception for the “this code should never be reached” case? Here’s an example:
void myMethod() {
int a;
... /* Some complex code that manipulates a. In the end, "a" can only be 1 or 2. */
switch (a) {
case 1: ...
case 2: ...
default: // just an extra sanity check
// Oops, we should never be here.
// There's apparently some bug in the code above.
throw new ThereIsABugInTheCodeException();
}
}
I don’t think creating my own exception for such a rare case is justified. On the other hand, just throwing Exception is discouraged as well. I guess some kind of AssertionFailedException would be appropriate, but I did not find any in the .net Framework. (Note that Trace.Assert does not throw an exception, so that’s not an option either.)
EDIT: To clarify: a is NOT an argument. Let me rephrase the question to make it clearer: In the middle of a method I do a sanity check (just to make sure, it should not be necessary if the code in the beginning of the method is correct). I find out the sanity check fails and would like to throw an exception. Which one do I throw? I don’t think that ArgumentException is the correct answer, since ArgumentException should only be thrown if the caller of the method did something wrong. That is not the case here. The method itself realizes at runtime that it contains a bug.
Oddly enough the .NET Framework does not contain an exception to describe logical or internal errors. But on the other hand the .NET Framework should have a quality level where there shouldn’t be a need to throw exceptions for internal errors. Would you trust a .NET API call that in the documentation stated that it might throw an
InternalErrorException? Remember that exceptions derived fromSystemExceptionare exceptions used by the framework. It can be useful to reuse these exceptions but the exceptions were created for use by the framework and not your framework/application.You can use
ArgumentException,ArgumentNullException,ArgumentOufOfRangeException,NotSupportedExceptionandInvalidOperationExceptionin cases where your methods a called in an unexpected way.You can also create your own
InternalErrorException.Microsofts more general solution to this problem is Code Contracts. Many contracts can be checked at compile time helping you produce better code and you can use
Contract.AssumeandContract.Assertto verify conditions in your code. The rewriter will insert code that will throw aContractExceptionif the condition is false. This exception has been crafted in a special way (from the DevLabs Code Contracts documentation):