C# User-Defined Exceptions

Last Updated : 29 Jun 2026

In the C# programming language, a custom exception is a user-defined class that inherits from the built-in System.Exception class. It allows us to create our own specific error types to handle special situations in the program. A common use case for custom exceptions is input validation.

C# User-Defined Exceptions

For instance, we can create an InvalidAgeException to throw an error when a user enters an age below 18, which may be invalid in certain business rules, such as age-restricted registration.

Custom exceptions help us represent the specific errors in code. It makes it easier to understand and fix problems by giving clear and meaningful messages. It allows us to handle the different errors in different ways, which helps to make the code cleaner and more organized.

Creating a Custom Exception in C#

In the C# programming language, custom exceptions are created by defining a new class that inherits from System.Exception class or any of its derived types. It allows us to handle application-specific errors in a more meaningful and structured way.

Syntax

It has the following syntax:

In the given syntax, the MyCustomException inherits from System.Exception that includes three constructors, which enables us to specify a custom error message and optionally pass an inner exception.

Handling Custom Exceptions

In C#, custom exceptions can be handled in the same manner as built-in exceptions using a try-catch block. When a custom exception is thrown in the program, it can be caught in a catch block that mainly targets the custom exception type. It enables the developers to give more expressive and specific error handling for different cases.

Syntax

It has the following syntax:

In this syntax, we catch the CustomException that can be thrown by the DoSomething() method. It allows us to handle the specific error scenario in a controlled way, such as logging the error or providing a user-friendly message.

C# Custom Exception Handling Example

Let us take an example to illustrate the custom exception in C#.

Example

Compile and Run

Output:

Exception caught: Sorry, Age must be at least 18 years.
Program continues...

Explanation:

In this example, we create a custom exception called InvalidAgeException, which is thrown when the age is less than 18. If the age is less than 18, it throws exceptions with the message "Sorry, Age must be at least 18 years". Inside the main() method, the exception is handled using a try-catch block, which allows it to run smoothly without crashing.

Types of Exceptions in C#

In C#, there are mainly two types of exceptions as follows.

  • System Exception
  • Application Exception
C# User-Defined Exceptions

Here, we will discuss these custom exceptions one by one.

System Exception

In the C# programming language, the System.Exception is the base class for all exceptions in the .NET Framework. An exception represents an unwanted or unexpected event that occurs at runtime and affects the normal flow of a program. The examples of System Exceptions include NullReferenceException, IndexOutOfRangeException, DivideByZeroException, and FileNotFoundException. We can also create custom exceptions by inheriting from this class.

Let us take a simple example to demonstrate how a system-defined exception works in C#.

Example

Compile and Run

Output:

System Exception Caught: Attempted to divide by zero.

Explanation:

In this example, we demonstrate the system exception in C#. Inside the try block, the program attempts to divide an integer value num by zero, which is mathematically undefined. Hence, the operation throws an exception. When the exception is thrown, the control immediately jumps to the catch block. The catch block handles the error by printing an error message.

Examples of System-defined Exceptions

The following examples demonstrate how to handle common system-defined exceptions in C# by using the try and catch blocks.

Example 1: Handling IndexOutOfRangeException

This example shows how to handle an IndexOutOfRangeException that occurs when accessing an array element with an invalid index.

Compile and Run

Output:

ERROR!
Error: This index number is not available in the array.
Exception message: Index was outside the bounds of the array.
Program continues running after handling the exception.

Explanation:

In this example, we create an array named num with three numbers. After that, we tried to access the fourth item, which does not exist. The try-catch block catches the error and prints the error message without crashing the program.

Example 2: Handling NullReferenceException

This example demonstrates how to handle a NullReferenceException that occurs when trying to access a member of a null object.

Compile and Run

Output:

ERROR!
Error: You tried to use an object that is null.
Exception message: Object reference not set to an instance of an object
Program continues running after handling the exception.

Explanation:

In this example, a string variable t is declared and set to a null value, which means it has no value. When the program tries to call the ToUpper() method on this null variable, it throws a NullReferenceException because we cannot use methods on a null object. The exception is caught using a try-catch block and prints the error message without crashing the program.

Application Exception

In the C# programming language, an application exception is a class that is derived from the System.Excpetion. It is used to represent errors related to the application's own logic or rules, rather than system-level errors.

For example, if we define a rule that during a division operation, and if the divisor is an odd number, the program should throw an exception.

Let us take an example to illustrate the application exception in C#.

Example

Compile and Run

Output:

Application error message: Negative values are not allowed.

Explanation:

In this example, we define a custom exception Tpoint that inherits from Exception. In the main() method, if the number n is negative, it throws a custom exception. After that, the catch block handles it and shows a meaningful error message.