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:
The following table lists important exception classes available in .Net.
||Raised when a non-null argument that is passed to a method is invalid.
||Raised when null argument is passed to a method.
||Raised when the value of an argument is outside the range of valid values.
||Raised when an integer value is divide by zero.
||Raised when a physical file does not exist at the specified location.
||Raised when a value is not in an appropriate format to be converted from a string by a conversion method such as Parse.
||Raised when an array index is outside the lower or upper bounds of an array or collection.
||Raised when a method call is invalid in an object's current state.
||Raised when incompitible types are being converted.
||Raised when the specified key for accessing a member in a collection is not exists.
||Raised when a method or operation is not supported.
||Raised when program access members of null object.
||Raised when an arithmetic, casting, or conversion operation results in an overflow.
||Raised when a program does not get enough memory to execute the code.
||Raised when a stack in memory overflows.
||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.
||Provides details about the cause of the exception.
||Provides information about where the error occurred.
||Provides information about the series of exceptions that might have occurred.
||This property can hold the help URL for a particular exception.
||This property can hold arbitrary data in key-value pairs.
||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.
Points to Remember:
- 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.