What exception to throw at the wrong code path?

I find that I am writing several methods where there is a path to the code that should never have happened. Here is a simplified example:

double Foo(double x) { int maxInput = 100000; double castMaxInput = (double)maxInput; if (x < 0 || x > castMaxInput || double.IsNaN(x)) { return double.NaN; } double r = 0; for (double boundary = 1; boundary<=castMaxInput; boundary++) { if (x <= boundary) { r += boundary * (x + 1 - boundary); return r; } else { r += boundary; } } // we should never get here. throw new SomeException(); } 

The exception that makes the most sense is something like

 TheAuthorOfThisMethodScrewedUpException() 

Because this happens if we get to the end of the for loop. Unfortunately, using the method described above, the compiler does not look smart enough to understand that the code should never happen after the for loop. That way you can't just have nothing, or the compiler will complain that "not all code paths return a value." Yes, I could add return double.NaN after the loop in addition to it. But that would mask the source of the problem.

My question is: - is there an exception that would be appropriate?

+8
c # exception
source share
4 answers

I am using the InvalidOperationException class . This means that the application has reached a state in which it should not be.

 throw new InvalidOperationException("Invalid state."); 

You can also Debug.Assert something true or just Debug.Fail when execution reaches a certain point.

 Debug.Fail("This should never happen!"); 

But debugging assertions / failures does not work in release mode only if the DEBUG condition is defined. Depending on your requirements, is this desirable.

As @AlexD correctly points out , there is also a Trace class with appropriate Assert and Fail methods that will run at runtime to help isolate and fix problems without disrupting the running system when the Trace condition is defined (set by default on the Project Properties tab ").


By the way, to answer the question in the title: you can create your own exceptions if you want.

 [Serializable] public class TheAuthorOfThisMethodScrewedUpException : InvalidOperationException { private const string DefaultMessage = "The author of this method screwed up!"; public TheAuthorOfThisMethodScrewedUpException() : this(DefaultMessage, null) { } public TheAuthorOfThisMethodScrewedUpException(Exception inner) : base(DefaultMessage, inner) { } public TheAuthorOfThisMethodScrewedUpException(string message) : this(message, null) { } public TheAuthorOfThisMethodScrewedUpException(string message, Exception inner) : base(message, inner) { } protected TheAuthorOfThisMethodScrewedUpException( System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) { } } 

And throw it at people.

 throw new TheAuthorOfThisMethodScrewedUpException(); 
+6
source share

Do not throw a new exception (), this causes problems for code trying to catch exceptions. The general, specific exception you can use is the following:

 throw new InvalidOperationException("Appplication invariants violated"); 

It is assumed that you want the error to occur in production, assuming the error is better than launching a rocket and ending peace. Other developers would prefer to use a method that assumes that the invariant can be ignored during the production process, but not during development, and we don't care whether we end the world or not.

+2
source share

Easy to create custom exception

 public class TheAuthorOfThisMethodScrewedUpException: Exception { public EmployeeListNotFoundException() { } public EmployeeListNotFoundException(string message) : base(message) { } public EmployeeListNotFoundException(string message, Exception inner) : base(message, inner) { } } 

then

 throw new TheAuthorOfThisMethodScrewedUpException("I am so sorry, this should never happen call me for more info") 
+1
source share

Easy! Use a snippet of code!

Exception + TAB + TAB

And that will create a new exception for you. This snippet produces this.

 [Serializable] public class MyException : Exception { public MyException() { } public MyException(string message) : base(message) { } public MyException(string message, Exception inner) : base(message, inner) { } protected MyException( System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) { } } 

You only need to change the name and do it!;)

0
source share

All Articles