In the previous chapter we have learned about try catch and finally keyword these are the basic building block of exception handling. In this chapter we will learn a new keyword “throw” in exception handling. This keyword is used for creating custom exception class or creating user defined exception class in C#.

While creating custom exception class it is good practice to keep class name with the end of Exception word. For example, OutofStockException, NumberNotFoundException etc.

Let’s start a programming example first then we will explain how can you create your own custome exception and use it in your program.

Custom Exception Class in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace User_Defined_Exception_Example
{
    class Program
    {
        static void Main(string[] args)
        {
            int acceptorder;
            Console.WriteLine("Welcome to Shopping Site:\nHow many headphone you want to buy (Total 10 in Stock):");
            acceptorder = Convert.ToInt32(Console.ReadLine());
            try
            {
                if (acceptorder == 10 || acceptorder < 10)
                {
                    Console.WriteLine("Congratulations! You have bought {0} headphones", acceptorder);
                    Console.ReadLine();
                }
                else
                {
                    throw (new OutofStockException("OutofStockException Generated: The number of item you want to buy is out of stock. Please enter total item number within stock"));
                }
            }
            catch (OutofStockException oex)
            {
                Console.WriteLine(oex.Message.ToString());
                Console.ReadLine();
            }
        }
    }
    //Creating Custome Exception - OutofStockException
    public class OutofStockException : Exception
    {
        public OutofStockException(string message)
            : base(message)
        {
        }
    }
}
Output 1

Input : 4
It will not through any exception and shows output.

Output 2

Input : 12
It will through OutofStockException and handled by OutofStockException catch block.

public class OutofStockException : Exception
    {
        public OutofStockException(string message)
            : base(message)
        {
        }
    }
1. In this below we have created a custom exception class OutofStockException which is catches by catch block when user enter larger number than the stock available. All the user defined exception class must be derived from its base class Exception.
public class OutofStockException : Exception
    {
        public OutofStockException(string message)
            : base(message)
        {
        }
    }
2. All the exception class must have a constructor. However, you may define numbers of constructors based on your requirement. Below we have created only one constructors to keep program simple understand. This constructors will be used later to throw a string message.
public class OutofStockException : Exception
    {
        public OutofStockException(string message)
            : base(message)
        {
        }
    }
3. Now in the main method, we asks user for buying a headphone and asks the total number of headphone he/she want to buy. If the number of buying headphone is equal or less than the stock a congratulations message appear but if the total number of buying headphone is larger than stock available it raise OutofStockException.
try
   {
     if (acceptorder == 10 || acceptorder < 10)
     {
     Console.WriteLine("Congratulations! You have bought {0} headphones", acceptorder);
     Console.ReadLine();
     }
     else
     {
     throw (new OutofStockException("OutofStockException Generated: The number of item you want to buy is out of stock. Please enter total item number within stock"));
     }
   }
catch (OutofStockException oex)
   {
     Console.WriteLine(oex.Message.ToString());
     Console.ReadLine();
   }

One response on “User Defined Exception Class In C#

  1. As you will inevitably learn on your path to losing weight, effective weight loss is not only about watching what you eat, but much more about changing your lifestyle. This means changing your habits and how you approach your day-to-day life. Read this information to help you throughout the process.

Leave a Reply

Your email address will not be published. Required fields are marked *