Home > DeveloperSection > Interviews > How to create user defined Exception ?

Posted on    September-19-2016 7:07 AM

 C# C#  Exception Handling 
Ratings:
 1 Answer(s)
  81  View(s)
Rate this:

Abhishek Srivasatava
Abhishek Srivasatava

Total Post:70

Points:350
Posted on    September-19-2016 7:07 AM


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();
        }
    }
}

Modified On Sep-25-2016 11:38:45 PM

Don't want to miss updates? Please click the below button!

Follow MindStick