Friday, April 16, 2010

Exception Handling in C#

"EXCEPTION IS A RUNTIME ERROR WHICH ARISES BECAUSE OF ABNORMAL CONDITION IN A CODE SEQUENCE. "

The try block contains the code segment expected to raise an exception. This block is executed until an exception is thrown The catch block contains the exception handler. This block catches the exception and executes the code written in the block. If we do not know what kind of exception is going to be thrown we can simply omit the type of exception.  

The finally block is used to do all the clean up code. It does not support the error message, but all the code contained in the finally block is executed after the exception is raised. We can use this block along with try...catch and only with catch too. The finally block is executed even if the error is raised. Control is always passed to the finally block regardless of how the try blocks exits.


There are a number of exception classes provided by C#, all of which inherit from the System.Exception class. Following are some common exception classes:

  • Exception Class - - Cause
  • SystemException - A failed run-time check;used as a base class for other.
  • AccessException - Failure to access a type member, such as a method or field.
  • ArgumentException - An argument to a method was invalid.
  • ArgumentNullException - A null argument was passed to a method that doesn't accept it.
  • ArgumentOutOfRangeException - Argument value is out of range.
  • ArithmeticException - Arithmetic over - or underflow has occurred.
  • ArrayTypeMismatchException - Attempt to store the wrong type of object in an array.
  • BadImageFormatException - Image is in the wrong format.
  • CoreException - Base class for exceptions thrown by the runtime.
  • DivideByZeroException - An attempt was made to divide by zero.
  • FormatException - The format of an argument is wrong.
  • IndexOutOfRangeException - An array index is out of bounds.
  • InvalidCastExpression - An attempt was made to cast to an invalid class.
  • InvalidOperationException - A method was called at an invalid time.
  • MissingMemberException - An invalid version of a DLL was accessed.
  • NotFiniteNumberException - A number is not valid.
  • NotSupportedException - Indicates sthat a method is not implemented by a class.
  • NullReferenceException - Attempt to use an unassigned reference.
  • OutOfMemoryException - Not enough memory to continue execution.
  • StackOverflowException - A stack has overflown.

0 comments: