In c#, Exception is an unexpected event or an error that may occur during the program's execution, and it will provide necessary information about the error which occurred in our application.
Generally, the exceptions are thrown by the .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#.
If you observe the example, we assigned a null value to the name variable and validated 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# 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 class will identify the exception type 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#.
If you observe the above diagram, the SystemException is a base class for all CLR exceptions, and these will occur during the program's execution. The ApplicationException is a base class for application-related exceptions, and we can derive this class from creating our own exception classes.
The 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 a 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 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 outside of its bounds.|
|InvalidCastException||This exception occurs for invalid casting or explicit conversion.|
|NullReferenceException||This exception will occur when we try to reference an object of NULL type.|
|OutOfMemoryException||This will occur when the program is not having enough memory to execute the code.|
|OverflowException||This will occur when 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 arbitrary data in key-value pairs.|
|HelpLink||It will hold a help file URL that provides detailed information about the cause of the exception.|
|InnerException||This property will provide information about the series of exceptions that might have occurred.|
|Message||It will provide 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 provides 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.