blog

Home / DeveloperSection / Blogs / Exception Handling in Java: Basic Concepts

Exception Handling in Java: Basic Concepts

zack mathews1429 20-May-2016

Looking at Murphy’s Law, “If anything simply cannot go wrong, it will anyway,” we know that we should always prepare ourself for the worst-case scenario.

Things don’t always turn out how we expect them to, and if anything can go wrong, it probably will. When this happens in real life, there may not be a remedial solution. Fortunately, in programming, if anything goes wrong, there is always a remedial action for us to take—this is called exception handling. When something goes wrong in our program, we say that an exception occurred. When we take a remedial action on it, we say that we handled the exception. Here is a famous quote by Douglas Adams:

The major difference between a thing that might go wrong and a thing that cannot possibly go wrong is that when a thing that cannot possibly go wrong goes wrong, it usually turns out to be impossible to get at or repair.

To substantiate this quote in sense of exception handling, let’s look at the most infamous computer bug in history. Due to an error in the software design, Ariane 5 Flight 501, which took place on June 4, 1996, failed within 40 seconds with a loss of a half-billion dollars. The reason? A tiny software bug. It occurred due to an exception thrown by some code that had originally been written for the earlier version of the rocket, Ariane 4. This particular routine, which could not be taken out easily, was left running although it was not really needed during flight. The code computed a big number that it tried to store in a short data type, causing an overflow condition. The program did not have a handler to catch this situation. Even an empty error handler would have probably saved the situation. But in the absence of an error handler, the error propagated to the operating system, which terminated the program. Unfortunately, the program was the guidance program for the rocket. All other computers on the flight had the same code and all of them crashed. Without any guidance, the rocket destroyed itself, which it was supposed to do in such a case.

From this story, we can certainly see the importance of exception handling. The proper use of exception handling can answer three basic questions:

·         What went wrong?

·         Where did it go wrong?

·         Why did it go wrong?


The type of exception informs us of “what” went wrong. The exception stack trace tells us “where,” and the exception message answers “why.” To make the best use of exceptions, we need to follow three recommended rules:

·         Be specific.

·         Throw early.

·         Catch late. 

We have likely experienced errors while running many off-the-shelf applications on our machine. When an application error occurs, probably the application terminates abnormally and we have to redo everything we have done so far after restarting the application. An application error may occur due to several conditions, some of which may be beyond our control. For example, a network connection may get disrupted or a file that the application is trying to open may not exist. The application may try to access an out-of-bounds memory location, or the .class file the application needs may be missing. An application can fail under several such conditions.


Updated 15-Mar-2018

Leave Comment

Comments

Liked By