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).

– 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.

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

//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


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


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


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


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


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


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


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


– OverflowException is thrown when an arithmetic operation overflows.


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


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


Leave a Reply

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

You are commenting using your 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: