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 .


3 comments:

  1. Hey ! This explanation by analogy was so cool ! And very interesting too ! :)

    ReplyDelete
  2. Very creative article! Software development is an intangible field in most cases, and I find that a lot of people have trouble understanding the concepts because there isn't anything physical about it that they can latch onto.

    Your comparison of programming in Java with your experience at the coffee shop is an excellent analogy, and if you explained your story to someone who wasn't a programmer, my guess is that it would definitely help them understand the concepts.

    I'll have to borrow your story if I ever have to explain error handling to someone whose never experienced it before.

    Great article!

    James

    ReplyDelete
  3. excellent analysis... the way human behavior is used in objective understanding fr a programmer is a point to appreciate...actually whenever we are searching for a solution of any problem..we commit a mistake always try to limit it within itself but solution is exists in front of us only...we need to just look at ...the way we want to.

    again i would like to say ..nice blog

    ReplyDelete