Sunday, September 16, 2012

An Exception View

Wikipedia Definition on Exception:

An action that is not part of ordinary operations or standards


From the definition it is clear that exception doesn’t mean error. In our day today life there are certain things which happens occasionally, and we handle it in different way [not in regular way].

For example, a festival is an occasion, which is not like our regular day to office, and we have a different plan for that special day and that is called an Exception.

Somehow we have the perception that Exception means something wrong/unexpected happened but which is not the case always



Exception Handling In Real Life – Human eye:

When an object appears near to your eyes with considerable force, the eyelid close automatically i.e., the Eye Lid is there to protect your eye, as it expects these situations to occur and force an action.


Exception Handling in Java:

The answer is pretty simple, put a try/catch block around the code which you feel is expected to fail / might need a substitute action.

In the above human eye example, the eyelid act as the try/catch block around the eye, which reacts on the unexpected

Example 1a:  A service to withdraw amount from the given account




The above snippet is self-explanatory and you can notice that method has throws InSufficientBalanceException in its signature. 

InSufficientBalanceException is expected to be thrown by the service whenever the withdraw amount is greater than balance amt. So now the caller is forced to handle this exception.


Example 1b:  Exception handling



The above client code calls the service [line 5] in a try / catch block [Line 4 – 10], if you have sufficient fund then transaction happens and the doll is yours [line 6], else the exception will be thrown by the service and it is logged in the catch block [line 9] and corrective action should be taken accordingly here the user can purchase by cash [Line 9]


This is what in java world we call it as Checked Exception / Compile time Exception, there is also another important type of exception called Unchecked Exception / Runtime Exception


 

 

 

 

 

 

 

 





So what is Unchecked Exception / Runtime Exception?


  • Is that it cannot/should not have corrective action? – Big NO
  • Is that because it occurs at runtime? - All Exceptions are expected to occur at runtime even the checked exception/compile exception so this cannot be the reason.

From the above answers it means, an Unchecked Exception can occur at runtime and it can be rectified/substitute action can also be applied but then this can be achieved with checked exception itself so why does unchecked Exception still exist?


Real world Example – Mobile and its insurance

When we purchase a mobile, it doesn’t come up insurance its up to the owner to decide whether it has to be insured or not.

If something happens for the uninsured mobile, it becomes purely the owner’s risk.
Some users feel the insurance is not required for them because of its low price, or the insurance doesn’t cover all the disabilities.

Similarly in java programming, the caller will be given the rights to handle/not to handle the exception i.e., compiler will not force them to write the try/catch block

If you can revisit Example 1a, you can see that retrieveAvailableBalance() method is called to get the available balance, assume if the account does not exist/not active then the service has to let the caller know about it.

Example 2a: Runtime Exception













You can see that the method do not have throws in its signature, that means the compiler will not force the caller of this method to put try/catch block i.e, eventhough we introduced new exception [InvalidAccountException], Example 1a & 1b still will compile without any change and it will works fine for a valid account








Now, the question what happens to the client/caller [Example 1b] for invalid account?

Yes, it will fail abnormally as the caller didn’t had the try/catch/rethrow when he calls the service, so the client code [Example 1b] should be something like below:


Example 2b: Exception Handling



Now we have the exception handling for invalid account is also in place.




















There are many other things in Exception, Hierarchies, Best Practices, etc that are not covered in this article as my primary focus is on Exception and not on the Exception API

I would recommend readers not to stop it here, there are many better-written articles available.
  


  
Below are some of the articles on Exception:

Anders Hejlsberg on checked vs. unchecked exceptions

http://www.artima.com/intv/handcuffs.html

James Gosling on checked exceptions

http://www.artima.com/intv/solid.html

Bill Venners on Exceptions

http://www.artima.com/interfacedesign/exceptions.html

Jacob Jenkov - Exception Handling

http://tutorials.jenkov.com/java-exception-handling/catching-multiple-exceptions.html

An Exceptional Philosophy:
http://www.dlugosz.com/Magazine/WTJ/May96/