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.
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.
- Nurali has solution to Nila’s problem
- Nurali has no idea about Nila’s problem
- 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.
- You know how to rectify the Exception
- You have no idea how to rectify the Exception
- 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.