C# allows us to create user-defined or custom exception. It is used to make the meaningful exception. To do this, we need to inherit Exception class, if you are new to exception handling in C#, read Exception handling in C# (With try-catch-finally block details) first ,as we will be using it's concept in this article to create custom exception in C#.

.NET provides a hierarchy of exception classes ultimately derived from the base class Exception. However, if none of the predefined exceptions meets your needs, you can create your own exception classes by deriving from the Exception class.

When creating your own exceptions, end the class name of the user-defined exception with the word "Exception", and implement the three common constructors, as shown in the following example.

This example defines a new exception class named StudentListNotFoundException. The class is derived from Exception and includes three constructors.

using System;

public class StudentListNotFoundException: Exception
{
    public StudentListNotFoundException()
    {
    }

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

    public StudentListNotFoundException(string message, Exception inner)
        : base(message, inner)
    {
    }
}

Now as you have got the basic of creating your own custom exception in C#, let create a complete example using Console application.

So, create a new project in your Visual Studio, by navigating to File->New->Project-> Select "Console Applicaiton" -> Enter the name "CustomExceptionHandling" -> Click "Ok"

In your program.cs use the below C# code

using System;

namespace CustomExceptionHandling
{
    class TestCustomException : Exception
    {
       
        public TestCustomException(string message) : base(message)
        {
            this.HelpLink = "Visit to qawithexperts.com for more information";
            this.Source = "This is source of Error";
        }
       
    }

    class MyClass
    {
        public static void Show()
        {
            throw new TestCustomException("This is Custom Exception example in C#");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                MyClass.Show();
            }
            catch (TestCustomException ex)
            {
                Console.WriteLine("Error Message:-" + ex.Message);
                Console.WriteLine("Hyper Link :-" + ex.HelpLink);
                Console.WriteLine("Source :- " + ex.Source);
                Console.ReadKey();
            }
        }
    }
}

In the above C# code we have created a Custom Exception "TestCustomException" which is derived from "Exception" class, by calling MyClass.Show() method, we are throwing the "TestCustomException" exception.

Here is the output:

Error Message:-This is Custom Exception example in C#
Hyper Link :-Visit to qawithexperts.com for more information
Source :- This is source of Error

c-sharp-custom-exception-handling-min.png

For best practices, you should always create an app which handles exceptions and errors to prevent app crashes. So, always use try/catch/finally blocks around code that can potentially generate an exception.

In catch blocks, always order exceptions from the most specific to the least specific and use a finally block to clean up resources, whether you can recover or not.