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

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 …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More