What is a Exception in c#


An application may encounter an error during the execution. When an error occurs, either CLR or program code throws an exception that 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:






Exception
NullReferenceException

Important Exception Classes:

The following table lists important exception classes available in .Net.
ExceptionDescription              
ArgumentException                                                                           Raised when a non-null argument that is passed to a method is invalid.
ArgumentNullExceptionRaised when null argument is passed to a method.
ArgumentOutOfRangeException                                                         Raised 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.


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.

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.

Post a Comment

0 Comments