Concept Of Custom Exception Or User Defined Exception In C#

Now we will learn about the concept of custom exception or user defined exception in C#.net with code snippets

Let us start.

First of all it is important to note that,

  • Exception handling in C#.net is based on these keywords
    try, catch, throw, finally.
  • Exception=>It is base class for all predefined exceptions

A few predefined exceptions are:

  • DivideByZeroException,
  • IndexOutOfRangeException,
  • OutOfMemoryException

Let us see System.DivideByZeroException with code snippets:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace CustomExceptionExampleCode {  
  7.     class Program {  
  8.         static void Main(string[] args) {  
  9.             int a = 5;  
  10.             int b = 5;  
  11.             try {  
  12.                 int k = 10 / (a - b);  
  13.             } catch (DivideByZeroException e) {  
  14.                 Console.WriteLine(e.Message);  
  15.             }  
  16.             Console.Read();  
  17.         }  
  18.     }  
  19. }   

The output of above program is=>

I attempted to divide by zero.

Explanation

In the above program it is going to fire an inbuilt message which is mentioned in predefined exception DivideByZeroException.

Above message is fired when user tries to attempt to divide by zero.

Now let’s see how to create our own or custom exception

A few important points need to be remembered here about creating custom exceptions-

First of all we need to derive our own class. We can say MyException class from Exception class is a base class for it.

Inside this MyException class we will be able to replace the Message of base class with our own custom message.

Let us see in detail with a code snippet, 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace CustomExceptionExampleCode {  
  7.     class MyException: Exception {  
  8.         public MyException(string Message): base(Message) {}  
  9.         public MyException() {}  
  10.         public MyException(string Message, Exception inner): base(Message, inner) {}  
  11.     }  
  12.     class Program {  
  13.         static void Main(string[] args) {  
  14.             int a = 50;  
  15.             int b = 10;  
  16.             int k = a / b;  
  17.             try {  
  18.                 if (k < 10) {  
  19.                     throw new MyException("value of k is less than 10");  
  20.                 }  
  21.             } catch (MyException e) {  
  22.                 Console.WriteLine("Caught My Exception");  
  23.                 Console.WriteLine(e.Message);  
  24.             }  
  25.             Console.Read();  
  26.         }  
  27.     }  
  28. }   

The output of above program is=>

It caught my exception.

The The below line of code demonstrates how to fire our own exception 

  1. if (k < 10) {  
  2.     throw new MyException("value of k is less than 10");  
  3.  

And also the below line of code demonstrates how to catch our own exception and fire our own message 

  1. catch (MyException e) {  
  2.     Console.WriteLine("Caught My Exception");  
  3.     Console.WriteLine(e.Message);  
  4. }   

Summary

We can handle our own exception handling by deriving our own class from Exception as a base class for it.

Next Recommended Reading Custom Exception in C#