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

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …