Custom Exceptions in c#
Custom exceptions in the C# programming language allow C# users to create their own custom exception error messages. Custom exceptions are unique to each C# user, tailored to the needs of their application. They allow C# users to provide more complex, detailed, and only essential exception error information. This makes it easier for C# programmers to debug existing program exception errors and handle or manage special program error conditions.

Why use custom exceptions in C# programming?
- Simply put – Custom exceptions in C# programs can provide more essential program error messages tailored to the current context of your program application.
- Speciality – Built-in exceptions, like C# program exceptions, are basic methods and do not indicate the specific error exceptions generated in a C# program application. Custom exceptions in class programs help C# programmers create error types that are directly connected to their application logic.
- Better Control – C# programmers can add custom properties or manual user-defined class methods to their own exception error class, which provide additional data information related to exception errors in the current program.
Creating a Custom Exception in C#.
To create a custom program exception error in a C# program, you can follow the steps below.
Creating a Custom Exception Steps.
Inherit from Exception – A C# user-defined custom exception class should inherit from the base exception class or a more specialized subclass of Exception, such as ApplicationException.
Specify Constructors – A basic method for C# users to create as many constructors as needed in their user-defined custom exception class. For example, exception classes exist in programs.
- They can declare a parameterless constructor.
- They can contain a constructor that accepts input messages from the C# user.
- They can contain a user-defined constructor that accepts both user messages and inner exceptions as input.
Add additional properties (optional) – C# users can customize the properties of exception errors to provide more detailed information about exception errors in programs, such as additional data or context for the error, for special use cases.
Basic example of a custom exception class in C#.
So, let’s create a custom exception error class in a C# program to handle or manage minimum depositor funds in a banking system.
Creating a custom exception in a C# program: InsufficientDepositException.
using System;
public class InsufficientDepositException : Exception
{
// Here we create a constructor that takes a custom user message.
public InsufficientDepositException(string message)
: base(message) { }
// Here we create a constructor that takes a custom message and an inner exception error.
public InsufficientDepositException(string message, Exception inner)
: base(message, inner) { }
// Here we define a default constructor.
public InsufficientDepositException() { }
}
Explanation of InsufficientDepositException.
- In this expression, the InsufficientDepositException class inherits from the Exception base class.
- Three constructors are created for this exception.
- One is the default constructor, which takes no parameters.
- It also has a constructor that accepts a custom error message as input.
- This is a user-defined constructor that passes both a custom exception error message and an inner exception to the current expression. This is useful for chaining exceptions within this expression.
Using a Custom Exception in a C# Program
Here, in a C# program, we can use this custom exception in the UserAccount class to handle fund withdrawal attempts by an account holder when the account balance reaches the minimum.
Example of Custom Exception in a C#.
Using System;
// here we create a Custom Exception Class
class InsufficientDepositException : Exception
{
public InsufficientDepositException(string message) : base(message)
{
}
}
class UserAccount
{
private decimal Ac_balance;
public UserAccount(decimal lastBalance)
{
Ac_balance = lastBalance;
}
public void Withdraw(decimal amount)
{
if (amount > Ac_balance)
{
// here it Throw the custom error exception
throw new InsufficientDepositException(
“You don’t have funds to withdrawal this transaction.”
);
}
Ac_balance -= amount;
Console.WriteLine(“You Withdrawal this transaction successful. current balance is: ” + Ac_balance);
}
}
class Program
{
static void Main()
{
UserAccount account = new UserAccount(9999);
try
{
account.Withdraw(21000);
}
catch (InsufficientDepositException exception)
{
//Here it handles the custom program exception
Console.WriteLine(“Display Error – ” + exception.Message);
}
}
}
Custom Exception Program Explanation.
UserAccount class.
In this custom exception program, the Withdraw class method checks whether the withdrawal amount by the account holder exceeds the balance. If this happens, it throws an InsufficientDepositException exception error with a custom error message in the current program.
Main method.
The try-catch block in this program catches the InsufficientDepositException program exception error and prints an error message to the console screen as output.
Custom Exceptions Detailed Summary in C#.
- A user-defined custom exception in a C# program helps you create and customize the program’s error handling to be more detailed, meaningful, and specific for the unique purpose of your program application.
- In C#, you can create and manage a custom exception error by inheriting from the Exception main base class and creating the relevant class constructor.
- This provides C# users with additional context for adding custom class properties and methods to their exception program errors.
