How to create user defined Exception ?
1133
19-Sep-2016
Updated on 22-Sep-2020
Abhishek Srivasatava
19-Sep-2016Following 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: