c-sharp
  1. c-sharp-custom-exception

C# Custom Exception

In C#, you can create your own custom exceptions that provide more specific information about an error or exceptional event. Custom exceptions can be helpful in providing helpful information to the user, easing debugging and error handling, and helping you avoid code duplication. In this tutorial, we'll discuss how to create and use custom exceptions in C#.

Syntax

To create a custom exception in C#, you need to define a new class and inherit from the Exception class. The syntax for creating a custom exception is as follows:

class MyException : Exception {
   public MyException() { }
   public MyException(string message) : base(message) { }
   public MyException(string message, Exception inner) : base(message, inner) { }
}

In the example above, we created a custom exception called "MyException" that inherits from the Exception class. We then provide three constructors that call their respective base class constructors.

Example

Let's say we want to create a custom exception called "NegativeNumberException" that is thrown when a negative number is passed into a method that doesn't accept negative numbers. Here's how we can implement it:

class NegativeNumberException : Exception {
   public NegativeNumberException() { }
   public NegativeNumberException(string message) : base(message) { }
   public NegativeNumberException(string message, Exception inner) : base(message, inner) { }
   public NegativeNumberException(int value) : base($"Negative number passed: {value}") { }
}

Now, we can use it in our code:

void CheckPositive(int value) {
   if (value < 0) {
      throw new NegativeNumberException(value);
   }
}

Output

When we run the example code above and pass a negative number to the "CheckPositive" method, the NegativeNumberException will be thrown and the corresponding message will be displayed on the console.

Explanation

In the example above, we created a custom exception called "NegativeNumberException" that is thrown when a negative number is passed into a method that doesn't accept negative numbers. We then wrote a "CheckPositive" method that throws the exception when a negative number is passed to it.

Use

Custom exceptions can be used whenever you need to provide more specific information about an error or exceptional event. They can be helpful in providing better debugging and error handling, as well as avoiding code duplication.

Important Points

  • When creating a custom exception, you should inherit from the Exception class.
  • You can provide constructors to your custom exception that call their respective base class constructors.
  • Custom exceptions can provide more specific information about an error or exceptional event and can be helpful in debugging and error handling.

Summary

In this tutorial, we discussed how to create and use custom exceptions in C#. We covered the syntax, example, output, explanation, use, and important points of custom exceptions in C#. With this knowledge, you can now create custom exceptions that provide more specific information about an error or exceptional event.

Published on: