How to write User Defined exceptions in C#?

10,445

Solution 1

It is practically the same as it is in Java - you extend the Exception class.

In the most basic form.

public class CustomException : System.Exception
{
  public CustomException()
  {
  }

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

  public CustomException(string message, System.Exception innerException)
    : base(message, innerException)
  {
  }
}

To this, then add the data that you want to capture using either fields or properties.

Out of interest, before answering this question, I checked the Microsoft Design Guildlines on Custom Exceptions. Designing Custom Exceptions (MSDN)

  1. Avoid deep namespace hierarchies.
  2. If possible, derive from one the provided common base exceptions, i.e., ArgumentException. HOWEVER Do not derive from ApplicationException. It is not harmful, it is no point in doing so. MSDN Blog Post on ApplicationException.
  3. Use the "Exception" suffix.
  4. Make exceptions serializable by implementing the ISerializable interface. Apparently an exception must be serializable to work correctly across application domain and remoting boundaries.
  5. Store security-sensitive information in a private exception state, add a SecurityPermission attribute to ensure that only trusted code can get the information.

I highly recommend reading the Design Guidelines on MSDN.

Solution 2

You want to inherit from System.Exception and preferrably provide (at minimum) the same public constructors and pass the parameters on to the base constructors. Add the relevant properties and/or methods you deem appropriate for your particular need.

public class MyException : System.Exception
{
     public MyException() : base() { }
     public MyException(string message) : base(message) { }
     public MyException(string message, Exception innerException) : base(message, innerException) { }
} 
Share:
10,445
Hemant Kumar
Author by

Hemant Kumar

Passionate about developing community, evangelizing technology and, giving enthusiastic technical presentations with exceptional skills at developing and fostering personal relationships. Having working professional having 7+ years of experience on .NET technologies like C#.NET,VB.NET,ASP.NET MVC,jQuery,JavaScript,HTML,AngularJs and CSS. I can do this task with in the given time-line as per the requirement. Beyond application development, I am skilled in high-level requirements gathering, risk analysis, technical design analysis, application modelling, implementation planning, and support.

Updated on August 18, 2022

Comments

  • Hemant Kumar
    Hemant Kumar over 1 year

    hi can any one tell me how to write user defined exceptions in C#?As we have in Java can we write in C#?

  • Jonathan Allen
    Jonathan Allen over 13 years
    According to the .NET Framework Design Guidelines, you are supposed to inherit directly from Exception unless you have a really good reason to pick something else.
  • this. __curious_geek
    this. __curious_geek over 13 years
    @Jonathan: All systems back to normal. Deleted my stupid answer and removed my useless comment. Upvoted the answer in the name of SO spirit. Thanks for correction.