Error Handling



Software that never fails is something near to impossible!”



Exception Handling

Just imagine a situation, we are showing a demo of our project to the client,then all of sudden the inevitable thing happens.
one dialog pops with some cryptic messages,that proves that our project is crashed.We wish that this should never happen, but in the real world, anything can happen at any time.We need to be Prepared.Just like wearing a helmet while riding.

When i was googling about this topic .I came across a real world example.Simple one, still very useful


Real world Example.

When we ride a cycle or bike we use a helmet to protect us from head injuries(Exceptions),Similarly we use life jackets when we go for boating.
An accident is unpredictable.we must be prepared for it at any time.

Helmet only protects the head. rest of the body is not protected.The part that need to be protected is placed inside safety ensuring object(helmet).

These objects are used to ensure our better safety.While riding a bycycle we dont wear both helmet and life jackets together. it will really look odd.we just need a helmet.



Programming World

Now Coming back to programming world.

We have exception handling (Helmets, lifejackets) will protect your application from crashing when there is an exception.the code that might throw exception is wrote inside the try block

Similarly, in Exception handling, you do not need to catch all exceptions. You need to catch only the ‘expected’ exceptions. Which means,if you are doing an arithmetic calculation, We need to use ArithmeticException (Helmet for Riding).


System.ArithmeticException
– This is the base class for exceptions that occur during arithmetic operations

To handle exceptions we have a class in dot net .The exception class .The exception class is a member of the System namespace and is the base class for all exceptions

• Once an exception occurs within the try block, the flow of control immediately jumps to an associated exception handler, if one is present.

	try
{
// Code which can cause an exception.
}
catch(Exception ex)
{
// Code to handle exception
MessageBox.Show ( ex.Message );


}
finally
{
//finally Block contains the code that always executes, whether or not any exception occurs.
}

In .NET, all exceptions are derived from System.Exception. So,catch (Exception) will catch all posibble exceptions in your .NET application.

• An exception handler is a block of code that is executed when an exception occurs. In C#, the catch keyword is used to define an exception handler.

• If no exception handler for a given exception is present, the program stops executing with an error message.
• If a catch block defines an exception variable, you can use it to get more information on the type of exception that occurred.

• Exceptions can be explicitly generated by a program using the throw keyword.

• Exception objects contain detailed information about the error, including the state of the call stack and a text description of the error.
• the code within the ‘finally’ block is executed whether there is an exception or not.



Common .NET Exception classes



System.ArithmeticException

– This is the base class for exceptions that occur during arithmetic operations, such as System.DivideByZeroException and System.OverflowException.


System.ArrayTypeMismatchException

– ArrayTypeMismatchException is thrown when a an incompatible object is attpemted to store into an Array.



System.DivideByZeroException

– This exception is thrown when an attempt to divide a number by zero.


System.IndexOutOfRangeException

– IndexOutOfRangeException is thrown when attempted to access an array using an index that is less than zero or outside the bounds of the array.



System.InvalidCastException

– Thrown when an explicit type conversion from a base type or interface to a derived type fails at run time.


System.NullReferenceException

– This exception is thrown when an object is accessed but it is null.


System.OutOfMemoryException

– OutOfMemoryException is thrown if the ‘new’ operation (creating new object) fails due to in sufficient memory.


System.OverflowException

– OverflowException is thrown when an arithmetic operation overflows.



System.StackOverflowException

– StackOverflowException is thrown when the execution stack is exhausted by having too many pending method calls, most probably due to infinite loop.



ArgumentException

– The exception that is thrown when one of the arguments provided to a method is not valid.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: