how to write a custom exception ?

Recommended Answers

All 3 Replies

Create an exception class that derives from whatever general exception already in place. Define your custom exception's way to handle the exception you want. Implement it in your code the same way as the provided exception handling.

Pseudocode:

Class MyCustomException : System.Exception
{

     properties;
         
     if( condition == true )
         {
              //handle exception;
         }
     else if ( anotherCondition == true )
         {
              //handle exception in another way;
         }
     else
         {
              //handle exception another way;
         }
}

//implementation

try
{
      doSomething();
}
catch(MyCustomException ex)
{
      ex.Message();
}

Do not copy this code, it will not work. It is just to give you the general idea of how to go about creating and implementing custom exceptions.

thanks !! :)

No problem, hope it helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.