---
title: "How to create user defined Exception ?"  
description: "How to create user defined Exception ?"  
author: "Abhishek Srivasatava"  
published: 2016-09-19  
updated: 2020-09-22  
canonical: https://www.mindstick.com/interview/23047/how-to-create-user-defined-exception  
category: "c#"  
tags: ["c#", "exception handling"]  
reading_time: 5 minutes  

---

# How to create user defined Exception ?

Following is the step we need to follow to create user defined exception:\

Step 1 : Create a derived class and inherit it with exception base class

example // class universalerror : Exception

**Step 2 :** create a constructor for the derived class and pass the string parameter

syntax : public universalerror(String message) : base(message) // what is the need of this line. I will explain you through program in 3 step.

**step 3 :** Declaration for the exception is made by the above two steps. Now we need to define the exception.

Create another class under the new class create a function put the condition for this variable

```
example :class testexp    {        public int a;         public void strgerr()        {            if (a== 0)            {               
throw (new universalerror("Error, please check")); // Base Message when exception is meet                         }            else            {                Console.WriteLine("Universal error is not there");// Base Message when exception is not meet.                        }        }    }
```

Base message in step : 2 is to give the add on for the string message (which may define in the main program or in executing function).

**step 4 :** Now create another class , create the object for the defining class of the exception.

**Step 5:** now with the help of three keyword Try & Catch

```
class Program    {         static void Main(string[] args)        {                      
testexp exp1 = new testexp();            try            {
              exp1.a = 25;               
             exp1.strgerr();            }            catch (universalerror e)            { 
                 Console.WriteLine("universal error {0}", e.Message);            }           
Console.ReadLine();        }    }} 
```

Under Try : we perform all operations.

Under Catch : if an exception is met , then we can give the add on to the defined message .

**Here is the complete program for the above step:**

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication3{    classuniversalerror : Exception    {        public universalerror(String message) : base(message)        {        }    }    class testexp    {        public int a;        public void strgerr()        {            if (a== 0)            { 
              throw (new universalerror("Error, please check"));                      }            else            {  
             Console.WriteLine("Universal error is not there");                   }        }    }    class Program    {        static void Main(string[] args)        {                  
            testexp exp1 = new testexp();            try            {               
            exp1.a = 0; //for exception is met.
            //exp1.a=25; for exception is not met.               
            exp1.strgerr();                     }            catch (universalerror e)            {
            Console.WriteLine( "universal error {0}",e.Message);            }           
            Console.ReadLine();        }    }}
```

## Answers

### Answer by Abhishek Srivasatava

Following is the step we need to follow to create user defined exception:\

Step 1 : Create a derived class and inherit it with exception base class

example // class universalerror : Exception

**Step 2 :** create a constructor for the derived class and pass the string parameter

syntax : public universalerror(String message) : base(message) // what is the need of this line. I will explain you through program in 3 step.

**step 3 :** Declaration for the exception is made by the above two steps. Now we need to define the exception.

Create another class under the new class create a function put the condition for this variable

```
example :class testexp    {        public int a;         public void strgerr()        {            if (a== 0)            {               
throw (new universalerror("Error, please check")); // Base Message when exception is meet                         }            else            {                Console.WriteLine("Universal error is not there");// Base Message when exception is not meet.                        }        }    }
```

Base message in step : 2 is to give the add on for the string message (which may define in the main program or in executing function).

**step 4 :** Now create another class , create the object for the defining class of the exception.

**Step 5:** now with the help of three keyword Try & Catch

```
class Program    {         static void Main(string[] args)        {                      
testexp exp1 = new testexp();            try            {
              exp1.a = 25;               
             exp1.strgerr();            }            catch (universalerror e)            { 
                 Console.WriteLine("universal error {0}", e.Message);            }           
Console.ReadLine();        }    }} 
```

Under Try : we perform all operations.

Under Catch : if an exception is met , then we can give the add on to the defined message .

**Here is the complete program for the above step:**

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication3{    classuniversalerror : Exception    {        public universalerror(String message) : base(message)        {        }    }    class testexp    {        public int a;        public void strgerr()        {            if (a== 0)            { 
              throw (new universalerror("Error, please check"));                      }            else            {  
             Console.WriteLine("Universal error is not there");                   }        }    }    class Program    {        static void Main(string[] args)        {                  
            testexp exp1 = new testexp();            try            {               
            exp1.a = 0; //for exception is met.
            //exp1.a=25; for exception is not met.               
            exp1.strgerr();                     }            catch (universalerror e)            {
            Console.WriteLine( "universal error {0}",e.Message);            }           
            Console.ReadLine();        }    }}
```


---

Original Source: https://www.mindstick.com/interview/23047/how-to-create-user-defined-exception

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
