User-Defined Exceptions in C#

C# User characterized special cases: In this instructional exercise, we will figure out how to make client characterized exemptions in C# with examples?

C# User-characterized special cases

In C#, we can make our special case class by acquiring base class Exception. Since as we realize that Exception is the base class for a wide range of special cases in C#.

Here is the example of a client characterized special case,

using System;

class UserDefineException: Exception
{

    public UserDefineException(string str)
    {
        Console.WriteLine(str);
    }
}

class Program
{
    static void Main()
    {
        UserDefineException M = new UserDefineException("User Define Exception");

        try
        {
            throw M;
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

Output:

User Define Exception
Exception of type 'UserDefineException' was thrown.

In the above program, we made a class “UserDefineException” by acquiring the base class “Special case” and made a parameterized constructor, through the client characterized object in “attempt block” of “Program” class, it is trapped in “get block”.

Leave a Comment

error: Alert: Content is protected!!