C# Exception

In c#, Exception is an unexpected event or an error which may occur during execution of the program and it will provide necessary information about the error which occurred in our application.


Generally, the exceptions are thrown by .NET framework CLR (Common Language Runtime) or by the code in our program. Following is the example which will throw an exception when we execute it in c#.


using System;


namespace TutlaneExamples


    class Program


        static void Main(string[] args)


            string name = null;

            // Null Reference Exception Error

            if (name.Length > 0)


                Console.WriteLine("Name: "+name);





If you observe the example, we assigned a null value to the name variable and trying to validate the length of the string variable.


When we execute the above c# program, we will get an exception like as shown below because the variable (name) value is null.


C# NullReferenceException Example Result


C# has provided necessary built-in exception classes to handle all the exceptions in our applications and all exception classes are derived from the base class called Exception. The Exception class will identify the type of exception and provide the properties that have details about the exception.


In c#, the Exception class further classified into two other classes called SystemException and ApplicationException to handle the exceptions. Generally, the SystemException is a base class for all CLR generated errors whereas the ApplicationException is a base class for all application related exceptions.


The following diagram will illustrate more details about the exception classes in c#.


C# System & Application Exceptions Hierarchy Diagram


If you observe the above diagram, the SystemException is a base class for all CLR exceptions and these will occur during execution of the program. The ApplicationException is a base class for application related exceptions and we can derive this class to create our own exception classes. 

C# Exception Classes

Following table lists some of the important exception classes available in c#.


AccessViolationException This exception will raise when there is an attempt to read or write protected memory.
ArgumentException This exception will be thrown when one of the arguments provided to a method is not valid.
ArgumentNullException This exception will occur when null argument is passed to a method that does not accept it as a valid argument.
ArgumentOutOfRangeException This exception will occur when the value of an argument is outside the allowable range of values.
ArithmeticException This exception will occur for an errors in arithmetic, casting, or conversion operation.
DivideByZeroException This exception will occur when we try to divide an integral or decimal value by zero.
FormatException This will occur when the format of an argument is invalid.
IndexOutOfRangeException This will occur when we try to access an element of an array or collection with an index that is outside of its bounds.
InvalidCastException This exception occur for invalid casting or explicit conversion.
NullReferenceException This exception will occur when we try to reference an object which is of NULL type.
OutOfMemoryException This will occur when the program is not having enough memory to execute the code.
OverflowException This will occur when an arithmetic, casting, or conversion operation results in an overflow.
StackOverflowException This exception will occur when the execution stack overflows.
TimeoutException This will occur when the time allotted for a process or operation has expired.

As we discussed, every exception class will derive from the base Exception class and provide the following important properties to get the details of an exception.


Data This property will hold an arbitrary data in key-value pairs.
HelpLink It will hold a help file URL that provides a detailed information about the cause of exception.
InnerException This property will provide an information about the series of exceptions that might have occurred.
Message It will provide the details about the cause of an exception.
Source It will set or get the name of an application or object that causes the error.
StackTrace It provide a stack trace to determine where an error occurred.

Generally, in c# we can handle all the exceptions by using try-catch blocks. In the next chapter, we will learn how to handle exceptions in c# with examples. 


Topics Covered