User defined exceptions are very common in programs and very helpful too. For instance, consider you have an Account class and on that class, you need to implement a custom exception. Consider the scenario where a user withdraws money and the amount he tries to withdraw is greater than his available balance. We can define a very simple exception inheriting the ApplicationException class

class InsufficientFundsException : ApplicationException
{
    public int errorCode;
    public InsufficientFundsException(int x)
    {
        errorCode = x;
    }
}
  
This takes an error code and will return it when catching the exception, so in the main program:

if (moneyToWithdraw > accountBalance)
    {
        throw new InsufficientFundsException(2001);
    }

and in try catch block,

try
{
    WithdrawMoney(moneyToWithdraw, accountBalance);
}
catch (InsufficientFundsException e)
{
    Console.WriteLine(e.errorCode);
    Console.WriteLine(e.Message);
}

Here, we print the errorCode and Message of the error.
blog1  

We see this error "Error in the application" because of the Message property of the exception is not set explicitly for a user defined exception, remember the C# compiler will assign Error in the application implicitly and its really not a good way to popup an error because the error message is so common.

blog2err


How to set a custom Message

There are two ways to set message, they are:

  1. Override the Message property
  2. Constructor Chaining

The first is rather lengthy and constructor chaining is a rather more elegant approach.

Override the Message property

class InsufficientFundsException : ApplicationException
{
    public int errCode;
    private string err_msg;
 
    public InsufficientFundsException(int err_no, string err)
    {
        this.errCode = err_no;
        err_msg = err;
    }
 
    public override string Message
    {
        get
        {
            return err_msg;
        }
            
    }
}

Look at the above code, in line 12, the property Message is overridden, since the original implementation of Message property is read-only, only get is implemented and passes a string.

Constructor Chaining

Using constructor chaining we can achieve this with only three lines of code.  The code also Overrides the Message property because many devs do not care for Constructor chaining. 

class InsufficientFundsException : ApplicationException
{
    public int errCode;
    public InsufficientFundsException(int err_no, string err)
        : base(err)
    {
        this.errCode = err_no;
    }
}

Here, in this approach, I just chain the current constructor of the exception InsufficientFundsException with the base class ApplicationException's constructor which accepts a string and set it as the Message. The final code is:

lusing System;
 
namespace Blog_Entry_app
{
    class Program
    {
        static void Main(string[] args)
        {
            int moneyToWithdraw = 10;
            int accountBalance = 5;
 
            try
            {
                Console.WriteLine(WithdrawMoney(moneyToWithdraw, accountBalance));
            }
            catch (InsufficientFundsException e)
            {
                Console.WriteLine(e.errCode);
                Console.WriteLine(e.Message);
            }
            Console.ReadKey();
             
        }
 
        static int WithdrawMoney(int moneyToWithdraw, int accountBalance)
        {
            if (moneyToWithdraw > accountBalance)
                throw new InsufficientFundsException(200,"Custom Message");
            else
                return accountBalance = accountBalance - moneyToWithdraw;
        }
    }
 
    class InsufficientFundsException : ApplicationException
    {
        public int errCode;
        public InsufficientFundsException(int err_no, string err)
            : base(err)
        {
            this.errCode = err_no;
        }
    }
 
}

 would print the custom error successfully.