C# Exception Handling3 min read

An exception is an event that occurs during the execution of the program i.e. during runtime. This event is unexpected, not known by the program, and disrupts the normal flow of the code. So to handle such errors, we create an exception handler code which will be executed when it finds an exception in a program.

Exception handling is a mechanism to maintain the normal flow of a program by handling runtime errors. The main advantage of exception handling is to ensure that even when an exception occurs, the program’s flow doesn’t break.


C# Exception Classes

The exception classes in C# are mainly directly or indirectly derived from the System.Exception class. Let us go through some of the predefined exception classes derived from the Sytem.SystemException.

ExceptionDescription
System.DivideByZeroExceptionIt handles the error generated by dividing a number by zero.
System.IndexOutOfRangeExceptionIt handles the error when the array index is out of range.
System.NullReferenceExceptionIt handles the error generated by referencing the null object.
System.ArrayTypeMismatchExceptionIt handles the error of array mismatched type.
System.InvalidCastExceptionIt handles the error generated during typecasting.
System.IO.IOExceptionIt handles the Input-Output errors.
System.OutOfMemoryExceptionIt handles the insufficient free memory error.
System.FieldAccessExceptionIt handles the error generated by invalid private or protected field access.
System.StackOverflowExceptionIt handles the error generated during stack overflow.

C# Exception Handling Keywords

C# provides four different keywords to handle the exception.

  • try: The try block identifies the block of code for which the exception will be activated. It is followed by a catch block.
  • catch: The catch indicates the cathching of an exception where you want to catch the error accuring in the program. It is placed right after the try block.
  • finally: Finally block is used to execute the code followed by the try-catch block. finally block is always executed whether an exception is handled or not.
  • throw: throw keyword is used to throw an exception when a problem shows up in a program.

C# try/catch and finally block

As discussed above, the try block contains the code that might throw an exception, catch handles an exception if there exists any, and finally block is used for any cleanup work that needs to be done. finally is always executed whether there exists an exception or not.

The syntax for a try, catch, and finally keyword.

Let us go through an example in C#.

Output:

System.DivideByZeroException: Attempted to divide by zero.
at....
Finally block is executed


C# User-Defined Exceptions

C# allows us to create our own exception in order to make it more meaningful. User-defined exception classes are derived from the Exception class.

Output:

InvalidAgeException: Age must be greater than 21

Go through throw keyword in C# topic next.


MORE

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More