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();
Virtlink
source share