Lession 34: 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:

Exception Class Hierarchy

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:

NullReferenceException

Important Exceptions:

The following table lists important exception classes available in .Net.
ExceptionDescription
ArgumentExceptionRaised when a non-null argument that is passed to a method is invalid.
ArgumentNullExceptionRaised when null argument is passed to a method.
ArgumentOutOfRangeExceptionRaised when the value of an argument is outside the range of valid values.
DivideByZeroExceptionRaised when an integer value is divide by zero.
FileNotFoundExceptionRaised when a physical file does not exist at the specified location.
FormatExceptionRaised when a value is not in an appropriate format to be converted from a string by a conversion method such as Parse.
IndexOutOfRangeExceptionRaised when an array index is outside the lower or upper bounds of an array or collection.
InvalidOperationExceptionRaised when a method call is invalid in an object's current state.
InvalidCastExceptionRaised when incompitible types are being converted.
KeyNotFoundExceptionRaised when the specified key for accessing a member in a collection is not exists.
NotSupportedExceptionRaised when a method or operation is not supported.
NullReferenceExceptionRaised when program access members of null object.
OverflowExceptionRaised when an arithmetic, casting, or conversion operation results in an overflow.
OutOfMemoryExceptionRaised when a program does not get enough memory to execute the code.
StackOverflowExceptionRaised when a stack in memory overflows.
TimeoutExceptionThe time interval allotted to an operation has expired.

Exception Class:

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.
PropertyDescription
MessageProvides details about the cause of the exception.
StackTraceProvides information about where the error occurred.
InnerExceptionProvides information about the series of exceptions that might have occurred.
HelpLinkThis property can hold the help URL for a particular exception.
DataThis property can hold arbitrary data in key-value pairs.
TargetSiteProvides 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:

  1. Exception is a base class for any type of exception class in C#.
  2. Exception types: SystemException and ApplicationException.
  3. SystemException class is used for CLR related runtime errors.
  4. Exception class includes important properties e.g Message, StackTrack, InnerException, data etc. to associate important information with every exception.

Comments

Popular posts from this blog

Display multiline text in razor

Lession 20: Create Layout View

Lession 54: Predicate delegate in C#