Exception in C#
An application may encounter an error during the execution. When an error occurs, either CLR or program code throws an exception which contains necessary information about the error. There are two types of exceptions in .Net, exceptions generated by the executing program and exceptions generated by the CLR.
C# includes built-in classes for every possible exception. All the exception classes are directly or indirectly derived from the Exception class. There are two main classes for exceptions - SystemException and ApplicationException. SystemException is a base class for all CLR generated errors whereas ApplicationException serves as a base class for all application related exceptions, which you want to raise on business rule violation.
The following is a hierarchy of some of the exception classes in .Net:
As you can see in the above figure, SystemException class is a base class for all the exception that can occurs during execution of the program. No other class derives ApplicationException class by default, because you as a programmer need to derive this class to create your own exeception classes as per the business rules.
The following figure shows how NullReferenceException is thrown in Visual Studio debug mode, when it accesses a property of a null object at runtime:
Important Exception Classes:
The following table lists important exception classes available in .Net.
|ArgumentException||Raised when a non-null argument that is passed to a method is invalid.|
|ArgumentNullException||Raised when null argument is passed to a method.|
|ArgumentOutOfRangeException||Raised when the value of an argument is outside the range of valid values.|
|DivideByZeroException||Raised when an integer value is divide by zero.|
|FileNotFoundException||Raised when a physical file does not exist at the specified location.|
|FormatException||Raised when a value is not in an appropriate format to be converted from a string by a conversion method such as Parse.|
|IndexOutOfRangeException||Raised when an array index is outside the lower or upper bounds of an array or collection.|
|InvalidOperationException||Raised when a method call is invalid in an object's current state.|
|InvalidCastException||Raised when incompitible types are being converted.|
|KeyNotFoundException||Raised when the specified key for accessing a member in a collection is not exists.|
|NotSupportedException||Raised when a method or operation is not supported.|
|NullReferenceException||Raised when program access members of null object.|
|OverflowException||Raised when an arithmetic, casting, or conversion operation results in an overflow.|
|OutOfMemoryException||Raised when a program does not get enough memory to execute the code.|
|StackOverflowException||Raised when a stack in memory overflows.|
|TimeoutException||The time interval allotted to an operation has expired.|
Every exception class in .Net is derived from the base Exception class. It includes the following important properties using which you can use to get information about the exception when you handle the exception.
|Message||Provides details about the cause of the exception.|
|StackTrace||Provides information about where the error occurred.|
|InnerException||Provides information about the series of exceptions that might have occurred.|
|HelpLink||This property can hold the help URL for a particular exception.|
|Data||This property can hold arbitrary data in key-value pairs.|
|TargetSite||Provides the name of the method where this exception was thrown.|
When an error occurs, either application code or the default handler handles the exception. Learn how to handle the excetion in the next section.
- Exception is a base class for any type of exception class in C#.
- Exception types: SystemException and ApplicationException.
- SystemException class is used for CLR related runtime errors.
- Exception class includes important properties e.g Message, StackTrack, InnerException, data etc. to associate important information with every exception.