Custom Exception Handling in C#

In C#, exception handling provides a mechanism to deal with errors or exceptional situations that can occur during the execution of a program. C# allows developers to create their own custom exceptions to handle specific error scenarios that are not covered by the built-in exception classes.

Creating Custom Exceptions

To create a custom exception in C#, you need to define a new class that derives from the System.Exception class. By doing this, your custom exception class will inherit all the properties and methods of the base Exception class, making it easy to customize the behavior as per your requirements.

Following is an example of creating a custom exception named InvalidInputException:

using System; public class InvalidInputException : Exception { public InvalidInputException() { } public InvalidInputException(string message) : base(message) { } public InvalidInputException(string message, Exception innerException) : base(message, innerException) { } }
User-Defined Exceptions in C# vb.net asp.net

In the above example, the InvalidInputException class is derived from Exception. It includes three constructors: a default constructor, one that takes a custom error message, and one that takes both a custom error message and an inner exception (used to represent the original exception that caused this custom exception).

Using Custom Exceptions

Once you have defined your custom exception, you can use it in your code to handle specific error scenarios. For example, let's say you have a method that calculates the area of a rectangle, and you want to throw a custom exception if the input parameters (length and width) are negative.

public class RectangleCalculator { public double CalculateArea(double length, double width) { if (length < 0 || width < 0) { throw new InvalidInputException("Length and width must be non-negative."); } return length * width; } }

In the above code, when the CalculateArea method is called with negative values for length or width, it throws an InvalidInputException with the specified error message.

Handling Custom Exceptions

How to handle custom exception in c#

To handle custom exceptions or any exceptions in C#, you use try-catch blocks. When an exception is thrown, the runtime searches for a catch block that can handle that exception type. If it finds a matching catch block, the code inside the catch block is executed.

public static void Main() { RectangleCalculator calculator = new RectangleCalculator(); try { double area = calculator.CalculateArea(-5, 10); Console.WriteLine("Area: " + area); } catch (InvalidInputException ex) { Console.WriteLine("Error: " + ex.Message); } catch (Exception ex) { Console.WriteLine("Unhandled Exception: " + ex.Message); } }

In this example, the Main method calls the CalculateArea method with negative values. Since the method throws an InvalidInputException, the catch block for InvalidInputException will handle it, and the custom error message will be displayed. If there were other types of exceptions, the generic Exception catch block would handle them.

Conclusion

Custom exceptions allow you to create more meaningful error messages and provide better context about what went wrong in your program. They make debugging and error resolution more efficient, especially when working on large-scale projects.