In C # how to define your own exceptions?

In C # how to define your own exceptions?

+55
c # exception
04 Feb '10 at 14:15
source share
5 answers

Recommendations on creating your own exception (next to the fact that your class should inherit from the exception)

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

     MyException (); MyException (string message); MyException (string message, Exception innerException); 

So, ideally, your custom Exception should look at a minimum:

 [Serializable] public class MyException : Exception { public MyException () {} public MyException (string message) : base(message) {} public MyException (string message, Exception innerException) : base (message, innerException) {} } 

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

CA1058: Microsoft.Design:
+ Change the base type to 'MyException' so that it no longer extends' ApplicationException. This base type exception does not provide any additional value to the structure of classes. Extend "System.Exception" or an existing type of unsealed exception instead. Do not create a new exception of the base type unless a value exists that allows you to create a capture handler for the entire exception class.

See the MSDN page for this rule.

+68
Feb 04 '10 at
source share



It seems that I started a little cue ball with the exception. Depending on the Microsoft Best Practices guide you follow ... you can either inherit from System.Exception or System.ApplicationException. There's a good (but old) blog post that tries to fix the confusion. I will save my example with Exception for now, but you can read the message and choose based on what you need:

http://weblogs.asp.net/erobillard/archive/2004/05/10/129134.aspx

No battle anymore! Thanks to Frederick for pointing out the FxCop CA1058 rule, which states that your Exceptions should inherit from System.Exception, not System.ApplicationException:

CA1058: Types Should Not Extend Some Base Types




Define a new class that inherits from Exception (I have included some constructors ... but you don't need them):

 using System; using System.Runtime.Serialization; [Serializable] public class MyException : Exception { // Constructors public MyException(string message) : base(message) { } // Ensure Exception is Serializable protected MyException(SerializationInfo info, StreamingContext ctxt) : base(info, ctxt) { } } 

And elsewhere in your code to throw:

 throw new MyException("My message here!"); 



EDIT

Updated with changes to provide Serializable exceptions. Details can be found here:

Winterdom Blog Archive - Make Serializable Exception Classes

Check out the section on the steps to take if you add custom properties to the Exception class.

Thanks to Igor for calling me!

+38
Feb 04 '10 at 14:17
source share

To determine:

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

To throw:

 throw new SomeException(); 
+13
Feb 04 '10 at 14:18
source share

Definition:

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

throwing:

 throw new CustomException("Custom exception message"); 
+3
Feb 04 '10 at 14:33
source share

You can define your own exception.

Custom exception classes are thrown from the ApplicationException class.

You can see the following code:

 using System; namespace UserDefinedException { class TestTemperature { static void Main(string[] args) { Temperature temp = new Temperature(); try { temp.showTemp(); } catch(TempIsZeroException e) { Console.WriteLine("TempIsZeroException: {0}", e.Message); } Console.ReadKey(); } } } public class TempIsZeroException: ApplicationException { public TempIsZeroException(string message): base(message) { } } public class Temperature { int temperature = 0; public void showTemp() { if(temperature == 0) { throw (new TempIsZeroException("Zero Temperature found")); } else { Console.WriteLine("Temperature: {0}", temperature); } } } 

and to exclude the exception,

You can throw an object if it is directly or indirectly derived from the System.Exception class

 Catch(Exception e) { ... Throw e } 
-3
Dec 06 '13 at 4:45
source share