Showing posts with label How excption occurs. Show all posts
Showing posts with label How excption occurs. Show all posts

Saturday, February 27, 2010

I am an Exception .....

Sunday afternoons are slow,calm,quite and peaceful. I usually like to spend my sunday afternoon at my favorite coffee shop sipping my favorite cold coffee(cappe nirvana),sitting beside the window watching time fly slowly.

On last sunday something unusual happened …. a couple in the coffee shop suddenly started arguing with the waiter for bringing the wrong order(to be honest poor waiter was not at fault,they placed the wrong order).As situation was about to get tensed ,suddenly cafe manager came into the picture from nowhere,listened to the argument patiently and handled the situation in a very diplomatic manner. This situation could have grown worse and it might have resulted in the feeling of sickness among the other customers also and they might have walked out disrupting the normal flow of business and afternoon also, but the situation handled by the cafe manager was remarkable.

Suddenly I realized that cafe is also a sequence of predefined actions(taking order,serving order,billing etc.),in other word it is also a program,and an exception occurred which was handled appropriately avoiding untoward situation. In the smooth flow of the cafe business suddenly the wrongly place order was about to break the flow,this event was similar to the “Exception ” in the programming.

Exception is shorthand for the “Exceptional Event” or in other words is the event that disrupts the normal flow of the program.
Whenever an exceptional event occurs within the method ,the method creates an object (which is called as Exception object )of it and hand it over to the runtime system ,this process is known as “throwing an Exception”.(In above scenario method was to placeOrder and exceptional event was wrong order)

The runtime system will try to find something to handle this exception object, in the Call stack, Call Stack as the name is the ordered list of all the methods that had been called ,in reverse order ,to get the method where exception occurred.
The runtime system actually looks for the Exception handler which is a method in the call stack that contains the block of code to handle the exception .(cafe manager played the runtime system and looking for the solution for the unexpected situation was similar to looking for call stack for the handler)

When an appropriate exception is found (by appropriate exception I mean if the type of the exception object thrown matches the type that can be handled by the handler.) runtime passes the exception to the handler. The exception handler is said to catch the exception.(when the manager handled the situation completely)

If the runtime exhaustively searches for the handler and if it doesn't find one one then comes the nightmare of any developer ….the flow terminates.

Exceptions are not the crime or something which is unforgivable but is some unexpected situation occurred which have caused the normal flow to disrupt.

So I have started behaving like a cool cafe manager. whenever I find an exception I coolly go to the method where exception occurred and say “hey how may I help you ?” and put my exception handling solution around it .