Friday, November 12, 2010

Exception Handling in Java

Exception Handling in Java is just like how it is in real life. Lets say you are driving from Princeton to New York.

  1. How would you handle the case where in your car meets with an accident.

  2. Can you do anything to avoid break failure.

  3. Can you do anything if an earth quake occurs on your way to New York ?

These are the three kinds of situation you come across in any program.

So, How do we handle exceptions ? How do you program your code with good exception handling ? This is the question that comes to any one's mind who has just learnt OOPS concepts in Java and is looking to learn more.

Exceptions are kind of thrown when they occur. If you catch them and and do something about it, the program continues smoothly with what it was doing. If you don't catch them, the program breaks and stops and you see red lines in the output. So, why would you not catch any exception ? Why would you allow it to break and stop a program. The reason is that there are kinds of exception which you are not supposed to caught. Confusing, right ? Read on....

Types of Exceptions:

There are two types of exceptions in Java. Checked Exception and Unchecked Exception. In other words, Compile time Exception and Runtime Exception. Yes, I am giving the other terminology because otherwise you will tell me that you have heard of them and what are those. There is double terminology for almost everything in Java. This makes Java look difficult, when its not actually easy. You might still be feeling that Java is difficult, never mind, lets continue on with Exception handling, so that I can show you that Exception Handling is easy.

Checked and Unchecked Exceptions:

The only two types of exceptions in Java are Checked and Unchecked. There is a class in Java called Exception, whose parent class(super class) is called Throwable. Throwable has two children, Exception and Error. Both Exceptions and Errors can be thrown literally. This is because they are children of Throwable and inherit that quality from it. We will talk about Errors later. We will continue on with Exceptions.

Class Exception has a lot of children classes(subclass). Exceptions are of two types, Checked and Unchecked Exception. So, how do you know which are checked and which are unchecked ? Class Exception has a child class called Runtime Exception. All its children and their children and so on... are Unchecked Exceptions. All the brothers/sisters of Runtime Exception and their families under them are Checked Exceptions. In other words, all the children(and families under them) of Exception class except Runtime Exception are Checked Exceptions. They are called Checked because compiler checks for them and forces you to decide on weather you want to catch and handle it or not (allow it to break and stop your program).

Example FileNotFoundException. Unchecked exceptions should never if handled, so the compiler does not force you to decide on its handling. If you find an unchecked exception during testing you are supposed to fix your program so that an unchecked exception never occurs. Example NullPointerException. They are like break failure. So when you find that break has failed during testing, go and fix it. You don't handle a break failure when it happens while running the Car. It should be fixed when found, so that it never happens upon driving.

Errors are like Earth Quake, like they happen due to a problem in JVM or physical machine. If they occur, they occur. You cannot do anything about them, accept that you report it to JVM creator or System Administrator.

Exception Handling Strategy: For the Checked Exceptions, the compiler will force you to do one of the two things. Either do a try catch finally or mention the throws clause at the end of method signature. For the unchecked exception, you have to fix them so that they never occur. For the Errors you cannot do anything. The Error will get displayed on the screen when program fails due to JVM problem or machine problem.

Summary:

In Java, Exceptions are either Checked Exceptions or Unchecked Exceptions. Checked Exceptions are checked by the Compiler and the compiler forces you to decide on weather you want to handle it or not. Unchecked Exception are not supposed to be handled. They should be fixed when found to occur in certain conditions in a program. I have not gone into examples as you will find the examples every where on the net. My attempt was to explain the exceptions in its totality and in a easily understandable way.

No comments:

Post a Comment