Exception handling Semantic (with Java)

Exception Handling is one of the most interesting topic for me.  During development, when I come across what to DO and NOT to do with Given Exception .. it interests me a lot.

Now, coming to the post title .. it’s NOT explaining syntax of exception handling in Java (i.e. explaining try, catch and finally keyword) .. but this post is meant to give idea of what to do with Given Exception.  This you may NOT get in any Java book which explain Java language.  This is some insight that I got from my experience which helps me in deciding what to do with Given Exception.

I want to explain this topic using one real life example.  In real life what is exception.  We can say I have a problem.  Problem of any sort .. but problem in real life is what exception in Java.  Below is scenario set-up for a intern who recently joined a company.

Scenario:

Nila recently join a company called Shivam Technologies Ltd.  Her manager is Abhi.  As Nila is new to company, Abhi has assigned a mentor named Nurali.  Here, Nurali’s responsibility is to help Nila to get on board with company and team.  So in case Nila has any problem (repeating the word ‘problem’) then she has to inform Nurali.

Nila is a problem creator so whenever she stuck somewhere in here job she will create Problem with its description and send problem to Nurali.  In Java, all third party s/w or library we used is something like Nila.  They stuck somewhere in execution and then they create Exception with its description and send to our program. (Try to link Nila as third party s/w or library who creates Problem and send to Nurali)

Now, there are different possibility wrt what Nurali can do with Nila’s problem.

  1. Nurali has solution to Nila’s problem
  2. Nurali has no idea about Nila’s problem
  3. Nurali has some extra information about Nila’s problem but he can’t solve her problem

In caes of first, Nurali will solve Nila’s problem.  For Second, Nurali has no idea about problem and he can’t solve it so he will forward problem to his manager Abhi. For third case, as Nurali has some extra information he will forward problem to Abhi with Extra information.

These are the same three choices for Exception Handling too .. interesting .. 🙂  Let’s link all three possible options when your s/w call any third party s/w or library.  When third party library throws Exception then you have three possible option what you can do with that Exception.

  1. You know how to rectify the Exception
  2. You have no idea how to rectify the Exception
  3. You have some extra information but you have no idea how to rectify the Exception

For first, you will write fallback logic in your catch block.  For ex, you call third party calculator library for division and you sent second input as Zero (0).  In this situation, calculator library will create Exception with description that ‘You cannot divide a number with Zero’ and the Exception will be thrown from calculator library.  Now, in your catch block as you know the second input was Zero, you can make sure to call once again calculator library with first argument as it is and second argument non-Zero (here, if you are taking input from user, you will inform exception description on UI and you will ask for second argument value and hopefully user will input non-Zero value).  So this was first case where you know the solution .. the solution is to send non-Zero value for second argument and the solution is achieved by once again asking second argument value from user.

For second, you have NO idea what to do with Given Exception.  Assume, you are developing Payment Gateway Application.  There is a movie booking website (for ex, bookmyshow.com) which call your payment service with information about Payment (like Amount, Customer Details, Bank Details etc).  Assume payment amount is Rs.2000/- and Customer account is with State bank of India (one of the bank in India).  Now, being Payment Gateway Provider, You have to initiate transaction to reduce customer balance in his bank account by Rs.2000/- to State Bank of India.  During this transaction, bank application found that the customer has balance of Rs.1000/- so the transaction of Rs.2000/- is not possible so bank application will create Exception something like InsufficientAmountException that you have to catch in your Payment Gateway application.  Now in this case you have no idea about InsufficientAmoutException so you will re-throw same exception (without modifying) to BookMyShow application.  Before re-throwing the InsufficientAmoutException you may add/update entry to track the transaction is cancelled due to insufficient fund in customer account.  So this is an example of second case where you re-throw the exception as it is.

For third, consider the same Payment Gateway Application which we have used for second case.  Now assume, Payment Gateway Application only throwing one Exception to BookMyShow application and that Exception is TransactionException.  It means Payment Gateway Service cannot re-throw InsufficientAmoutException as it is.  So what it will do.  In catch block of InsufficientAmoutException, it will create TransactionException and will set cause (one of the variable for Exception class) to InsufficientAmountException.  Also, TrancatoinException will have variable to store transactionCode so Payment Gateway Application also sets transactionCode variable with Transaction Code of payment transaction.  Now, this newly created TransactionException will be throw to BookMyShow application.  May be BookMyShow application will show user one line description which comes from root cause (i.e. InsufficientAmoutException) along with Trancation Code from TransactionException to the user.  So this example shows how root cause Exception of InsufficientAmoutException is wrapped with TransactionException and also added extra information/description.

This will end my this post.  Hope you have got idea about what to DO and NOT to do with Given Exception.

At last .. Of course, you will always add some logger statement in all three exception scenario.

For Exception handling, there are some variances like .. when to Eat the Given Exception ..   when to use Checked exception or UnChecked exception .. when to catch UnChecked Exception and when NOT to .. but all such thing are not cover in this post.

Advertisements
Posted in Uncategorized | Tagged , | Leave a comment

Collection – Micro blogging in one page

I thought of having one page where I will just put some good thing that I came across .. whether it’s related to technology or not.  It’s may be a good quote .. a good video link .. a good thought .. a good idea .. anything .. which I think I should keep somewhere so that at least I can re-refer it in future.

Videos:

Jeff Bezos – Regret Minimization Framework

Posted in Uncategorized | Leave a comment

PaaS (Platform As A Service)

Today, I googled PaaS for sometime.  I have never expected that PaaS is quite in light in Computer field.  I have came across lot of things on PaaS (particularly Java PaaS) and I think that PaaS will grow in future.

What is PaaS:

I think all XaaS are self descriptive.  I mean SaaS, PaaS, IaaS.  To run any Software/Application/Product, there are three thing required.  Application, Server/OS, Hardware.

Application is the actual program which will be used by end-user.  If you provide your application as a service means to use your application end-user has to pay something then it’s SaaS.

The next is Server/OS – Being company “ABC”, I want to deploy my web application on server but I don’t want to maintain Application Server, Database, OS on which my web application will run.  In this scenario, I will hire Platform service from one of the PaaS provider.

The last is Hardware, Being company “ABC”, I have my web application and I also have Application Server license.  I just don’t want to invest on Hardware part.  I will go for IaaS provider like Amazon.

Now particularly on PaaS, it’s not only about Application server or OS.  It’s about, I am a developer, I have some idea which I want to implement as my InnovativeApp.  From start of development of InnovativeApp, testing of App, build of App, deployment of App, monitoring of App .. everything I can get from PaaS provider.  In sort, I have my idea and that idea converted to InnovativeApp and user started using it .. everything can be done by service provided by PaaS.

Why PaaS:

Why IaaS – No need to invest on data center and hardware.  Just pay for number of hardware you use for given duration in hours.

Why SaaS – No need to purchase licence of Software. If I want to convert a PDF file to Word, I no need to get licence of my favorite software.  If my favorite software is available as a Service then I can use it by paying in hours basis (or may be number of time I use service basis)

Why PaaS – No need to purchase licence of Application server, Database.  No need to maintain Application Server.  If I have my web application ready, I can just deploy on PaaS.  Now, if number of hits on my web site increase then PaaS will take scalability for my web application.  In terns, I only need to pay in hourly basis.  And tomorrow I plan to shut down my site, I just need to un-registered from PaaS and remove my web application.

PaaS for Java:

There are many PaaS available which support deployment of Java application.  I don’t want to repeat it .. I found some good link for that.

InfoQ : A Java Developer’s Guide to PaaS

InfoQ : SAP’s OSGi-Based Java PaaS Achieves Java Enterprise Edition 6 Web Profile Compatibility

Posted in Uncategorized | Tagged , | 1 Comment

Eclipse Juno – Eclipes 4.2 – Eclipse 3.8

With the latest release of Eclipse – Eclipse Juno, create some confusion with two version of eclipse releases – Eclipse 4.2 and Eclipse 3.8.

I looked around internet and found some info which bring clarity on … Eclipse Juno – Eclipse 4.2 – Eclipse 3.8.

If you go to obvious link to download eclipse : http://eclipse.org/downloads/ It will say “Eclipse 4.2 Juno download”

From eclipse project plan link:  (Excerpt)

This document lays out the feature and API set for the next feature release of the Eclipse SDK after 3.7, designated release 4.2 and code-named Juno. This release is occurring simultaneously with the 3.8 platform release. The 4.2 release is a mature platform release containing significant new feature work, while the 3.8 release focuses on stability and bug fixes.

Also from Eclipse Juno Release Train Has Arrived: (Excerpt)

Eclipse 4.2 in now the mainstream platform for the Eclipse community. The existing Eclipse 3.x code stream is being put into maintenance mode. Eclipse 4.2 includes a compatibility layer that allows existing Eclipse plugins and RCP applications to work on the new platform.

So, Eclipse 4.2 is standard/mainstream release for this year from Eclipse and named as Eclipse Juno.

Also, Eclipse 3.8 is released this year but it has bug fixes over Eclipse 3.7 (Indigo).

Eclipse 3.x put into maintenance mode and now onwards Eclipse 4.x is standard/mainstream/release_train for Eclipse.

How does Eclipse 4.x differ from 3.x architecturally? (From Eclipse FAQ)

The Eclipse SDK 4.2, for the most part, contains all the plug-ins that make up Eclipse 3.8. That is, all of JDT and PDE, and most of the Platform, are the same bits as in 3.8. What’s different is the implementation of the Workbench, i.e. the org.eclipse.ui.workbench plugin, and the technologies this new implementation is based on. Before the release, the technologies (modeled UI, dependency injection and service-based programming model, CSS-based styling) were called ‘e4’ but we are now referring to them as the Eclipse 4 Application Platform or E4AP.  On top of the Eclipse 4 Application Platform, the 4.2 Workbench offers an implementation of the 3.x Workbench APIs, to provide backwards compatibility mainly for the Eclipse IDE.

So, Eclipse 4.2 is based on E4 architecture of modeled UI, dependency injection, service-based programming model, CSS-base styling.

Also Eclipse 4.2 provides 4.2 workbench which offers backward compatibility to 3.x Workbench.

In short, Eclipse 4.2 is in line of E4 releases and also has compatibility with Eclipse 3.x plugins and RCP.

In case, you don’t like Eclipse 4.2 default look & feel : To get classic look and feel (of Eclipse 3.x), Open Eclipse 4.2, go to Windows->Preferences->General->Appearance, Choose Theme-Classic and restart Eclipse.

Reference Links:

Eclipse FAQ – http://wiki.eclipse.org/Eclipse4/Eclipse_SDK_4.0_FAQ
Eclipse Juno Release Train Has Arrived – http://www.eclipse.org/org/press-release/20120627_junorelease.php
Project Plan For Eclipse Project, version Juno – http://www.eclipse.org/projects/project-plan.php?projectid=eclipse
Eclipse Juno and the Future of the Eclipse Platform – http://www.infoq.com/news/2012/01/eclipse-juno
Eclipse Juno Brings Eclipse 4 as Standard – http://www.infoq.com/news/2012/06/eclipse-juno
 
Download Eclipse 3.8 – http://download.eclipse.org/eclipse/downloads/drops/R-3.8-201206081200/index.php
Posted in Discussion | Tagged , | Leave a comment

List of Technologies with their tiny description and a link

Here I will try to maintain list of the technologies in hierarchy view:

  • Others:

SwitchYard – NextGen ESB (Enterprise Service Bus) from JBoss

Smooks – Smooks is an extensible framework for building applications for processing XML and non XML data (CSV, EDI, Java etc) using Java

PrimeFaces – Open source component library for JSF2 offering an extensive suite of Ajax enabled controls

Posted in Uncategorized | Leave a comment

Attended JUDCon2012:India at Bangalore on 24-25 Jan 2012

Hi Friends,

I attended JBoss Users & Developers Conference 2012 at Bangalore on 24-25 Jan 2012.

Here, I would like to share my expr as well the things I learnt in many different sessions.

Continue reading

Posted in Conference | Leave a comment

FirstThingsFirst

I would like to keep myself update with what’s happening in field of Computers .. and I keep referring to below sites most frequently ..  It has high quality contents on different topics .. Give 10 mins to each of them now .. and if you like it .. Give some quality time daily ..

  1. http://www.infoq.com/
  2. http://www.parleys.com/
xTra:
It falls to Life Long Education
Posted in Knowledge | Tagged , | Leave a comment