Exception Handling in C#

Exception Handling in C# is a process to handle runtime errors. We perform exception handling so that normal flow of the application can be maintained even after runtime errors. In C#, exception is an event or object which is thrown at runtime. All exceptions the derived from System.

Benefits of Exception Handling in C# is as follow:

(a) Exception handling can control run tune errors that occur in the program.

(b) It can avoid abnormal termination of the program and also shows the behavior of program to users.

(c) It can separate the error handling code and normal code by using try-catch block.

Exception Classes in C#

C# exceptions are represented by classes. The exception classes in C# are mainly directly or indirectly derived from the System.Exception class. Some of the exception classes derived from the System.Exception class are the System.ApplicationException and System.SystemException classes.

The System.ApplicationException class supports exceptions generated by application programs. Hence the exceptions defined by the programmers should derive from this class.

The System.SystemException class is the base class for all predefined system exception.

The following table provides some of the predefined exception classes derived from the Sytem.SystemException class −

Exception Class Description

1. System.IO.IOException Handles I/O errors.

2. System.IndexOutOfRangeException Handles errors generated when a method refers to an array

index out of range.

3. System.ArrayTypeMismatchException Handles errors generated when type is mismatched with the