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
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(); } } }
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
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
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
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: