Custom Exception class in C#

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 32311 red flag

How to implement Custom Exception in C#.

Custom Exception class in C#

In this article we will implement custom exception class with sample example. In C#, application exception handling plays a very crucial part. Without gathering sufficient information, it’s very tough to identify exception.  If we implement custom exception class then we can able to throw our own exception where needed.

How to implement?

At first we have to derive one class from ApplicationException class to implement custom exception in application. Then within derive class we can implement constructor as per our need.

Define custom class from ApplicationException Class

In below example, we have derived MyException class from ApplicationException class.  Within constructor we are calling constructor of base class with string argument. So, When the object will create it will contain this information. Have a look on  below code.

using System;
using System.Collections;
using System.Globalization;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Generic;
namespace Test1
{
    class MyException : ApplicationException
    {
        public MyException(String Msg):base(Msg)
        {
        }
    }
 
    class MyClass
    {
        public static void Show()
        {
            throw new MyException("This is My Exception");
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                MyClass.Show();
            }
            catch (MyException ex)
            {
                Console.WriteLine(ex.Message);
            }
 
            Console.ReadLine();
        }
    }
}

Output of this example is here



Set few properties in custom error object

The above example is very simple one and we did not set any property within object of MyException class. In this example we will see how to set few properties to object of custom exception class then it will provide more information related to error.

Here we will set HelpLink and Source property to object of  MyException class. Those information will get print within Main() function.

using System;
using System.Collections;
using System.Globalization;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Generic;
namespace Test1
{
    class MyException : ApplicationException
    {
        public MyException(String Msg):base(Msg)
        {
            this.HelpLink = "Visit to dummywebsite.com for more information";
            this.Source = "This is source of Error";
        }
    }
 
    class MyClass
    {
        public static void Show()
        {
            throw new MyException("This is My Exception");
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                MyClass.Show();
            }
            catch (MyException ex)
            {
                Console.WriteLine("Error Message:-" + ex.Message);
                Console.WriteLine("Hyper Link" + ex.HelpLink);
                Console.WriteLine("Source :- " + ex.Source);
            }
 
            Console.ReadLine();
        }
    }
}
 

Here is sample output


Conclusion:

In this example we have seen how to implement our own exception class in C# application. For demonstration purpose, we have just printed the error information but in real time scenario you may need to log it for further investigation. 


Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)