tags:

views:

268

answers:

4

In C# how do I define my own Exceptions?

+18  A: 
Justin Niessner
@Robin ...thanks for the edit. I went back in after I realized I forget to format as code and it was already fixed.
Justin Niessner
@Justin Niessner: You have to make your custom exception serializable if you want to be able to throw it across AppDomains
Igor Korkhov
It is not recommended to inherit from ApplicationException. It increases your inheritance hierarchy for no good reason. see http://blogs.msdn.com/brada/archive/2004/03/25/96251.aspx and http://blogs.msdn.com/kcwalina/archive/2006/06/23/644822.aspx
Venr
@Venr - Since MS always in conflicting on this issue, I usually use the guidelines found at http://weblogs.asp.net/erobillard/archive/2004/05/10/129134.aspx
Justin Niessner
Gabe Moothart
Igor Korkhov
MS is not conflicting in their guidelines regarding from which type you should inherit custom exceptions. Before .NET2.0 they advised to inherit from ApplicationException.Since .NET2.0, they advise to inherit from Exception. There's even an FxCop rule that enforces this. (see my answer on this question)
Frederik Gheysels
+7  A: 

To define:

public class SomeException : Exception
{
    // Add your own constructors and properties here.
}

To throw:

throw new SomeException();
Will Vousden
thank you!!!!!!!!!!!!!!!
gaurav shinde
+1  A: 

Definition:

public class CustomException : Exception
{
   public CustomException(string Message) : base (Message)
   {
   }
}

throwing:

throw new CustomException("Custom exception message");
+12  A: 

Guidelines for creating your own exception (next to the fact that your class should inherit from exception)

  • make sure the class is serializable, by adding the [Serializable] attribute
  • provide the common constructors that are used by exceptions:

    MyException ();

    MyException (string message);

    MyException (string message, Exception innerException);

    MyException (SerializationInfo info, StreamingContext context);

So, ideally, your custom Exception should look at least like this:

[Serializable]
public class MyException : Exception
{
    public MyException ()
    {}

    public MyException (string message) 
        : base(message)
    {}

    public MyException (string message, Exception innerException)
        : base (message, innerException)
    {}

    protected MyException (SerializationInfo info, StreamingContext context)
        : base (info, context)
    {}

}

About the fact whether you should inherit from Exception or ApplicationException: FxCop has a rule which says you should avoid inheriting from ApplicationException:

CA1058 : Microsoft.Design :
Change the base type of 'MyException' so that it no longer extends 'ApplicationException'. This base exception type does not provide any additional value for framework classes. Extend 'System.Exception' or an existing unsealed exception type instead. Do not create a new exception base type unless there is specific value in enabling the creation of a catch handler for an entire class of exceptions.

Click for the page on MSDN regarding this rule.

Frederik Gheysels