Exception Handling in C#
In Programming Exception handling is used to generate custom errors, and prevents the program to crash. It provides a way to transfer control when some error occurs. In this tutorial, you will learn Exception handling in C#. C# exception handling has four built-in keywords.
try: The main code is written in this block which we want to test. It can have more than one or one catch block to catch the exceptions.
catch: if is there some problem in try block like it will have some exception then the catch block catch it using its exception parameters.
finally: The finally block is used to execute a given set of statements, whether an exception is thrown or not thrown.
throw: A program throws an exception when a problem shows up. This is done using a throw keyword.
Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 |
try { } catch( Exception e) { } finally { } |
Exception Classes in C#:
C# provides built-in exception classes. i.e. System.Exception, which is further divided into Application Exception (System.ApplicationException) and System Exception (System.SystemException) classes.
System Exception is used for predefined System generated exceptions whereas Application Exception is user defined exceptions.
1. Built-in Exceptions (System Exception):
System.IO.IOException
It handles the input or output exceptions or errors.
System.IndexOutOfRangeException
It handles the exceptions occurring when an array is filled up but user tries to input the values in it i.e. Array index out of bound.
System.ArrayTypeMismatchException
It handles the exceptions occurring when the user tries to input different type of value into an array of some other type. For instance, when the user enters String type values in an integer type array.
System.NullReferenceException
It handles the exceptions occurring when there is any kind of dereferencing of an object or null object.
System.DivideByZeroException
It handles the exceptions occurring when some value is divided by zero, which causes an infinite answer.
System.InvalidCastException
It handles the exceptions occurring at type-casting. Conversion of data types.
System.OutOfMemoryException
It handles the exceptions occurring when there is not enough free memory.
System.StackOverflowException
It handles the exceptions occurring when there is any scenario of stack overflow.
Exception handling Example
Now understand this concept with an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
using System; using System.Exception; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program { int result; Program() { result = 0; } public void divid(int num1, int num2) { try { result = num1 / num2; } catch (Exception e) { Console.WriteLine("Exception found: {0}", e); } finally { Console.WriteLine("Result will be: {0}", result); } } static void Main(string[] args) { Program d = new Program(); d.divid(25, 0); Console.ReadKey(); } } } |
Exception Caught:
1 |
Exception Caught: System.DivideByZeroException: Attempted to divide by zero. |

As we know divide with zero is an error so we control it using try catch block and show exception in output screen.
2. User-Defined Exceptions (Application Exception)
You can also define your own exceptions in C# by creating a class for custom exception and extend that class from Exception Class.
Example
1 2 3 4 5 6 |
public class WrongInputException:Exception { public WrongInputException(string message): base(message) { } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class CheckInput { static void Main(string[] args) { Input inp = new Input(); try { inp.showInput(); } catch(WrongInputException e) { Console.WriteLine("WrongInputException: {0}", e.Message); } Console.ReadKey(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class Input { int UserInput = 0; public void showInput() { if(UserInput == 0) { throw (new WrongInputException("Wrong Input Found")); } else { Console.WriteLine("Input: {0}", UserInput); } } } |