Thursday, July 15, 2010

Exceptions in .NET Framework

A variety of exceptions are defined in .NET Framework under the System namespace.
These Exceptions are thrown when certain errors are found in your C# code or in the methods you call.
All of these exceptions can be caught using a catch block in C#.

Here are some of the common Exceptions:

  EXCEPTION CLASS   CAUSE OF EXCEPTION
SystemException It is used as a base class for other exceptions. Occurs on a failed run time check
AccessException Occurs when failed to access a type member (method or a field)
ArgumentException Occurs when an Argument to a method is invalid
ArgumentNullException Occurs when a null argument is passed to a method which does not accept it
ArgumentOutofRangeException Occurs when argument value is out of range
ArithmeticException Occurs due to arithmetic over-flow or under-flow
ArrayTypeMismatchException Occurs when you attempt to store different type of object in an array
BadImageFormatException Occurs when the image is in the wrong format
CoreException Its the BaseClass for the Exceptions thrown by runtime
DivideByZeroException Occurs when you attempt to divide by zero
FormatException Occurs if the format of an argument is wrong
IndexOutofRangeException Occurs when an array index is out of range
InvalidCastException Occurs when you attempt to cast to an invalid class
InvalidOperationException Occurs if a method is called at an invalid time
NotFiniteNumberException Occurs when a number is invalid
NotSupportedException Occurs if a method is not implemented by a class
NullReferenceException Occurs if you attempt to use an unassigned reference
OutofMemoryException Occurs if the memory is not enough for execution
StackOverflowException Occurs when stack overflows

2 comments:

  1. Thanks for the Info. I have a query on the order in which the exceptions should be caught. For ex, If I have a line of code which might throw 2-3 types of exceptions, is there any kind of order in which the exceptions should be caught?

    Code snippet ex:

    Try
    {
    string strText = strArrayValues[5].ToString();
    }
    Catch(...)
    {
    //catch code here
    }

    For the above line of code there might be the possibility of throwing index out of range or null reference or invalid cast exception. So is there any order in which we need to catch the type of exception occured from the code under try??

    Thanks in Advance.

    ReplyDelete
  2. Thank you for sharing this post..this is an informative blog post, it is great post regarding .Net FrameWork..thank you for the post..Custom Software Development Services

    ReplyDelete