Sunday, April 11, 2010

Exception

Exception:
In our real life, we often say that exceptions are everywhere, mean something really unusual which does not take place often and come rarely.
No matter how well prepare you are but you might face some problem for which you were not ready and you will say it came exceptionally.

Same thing happens in java programming, it hardly matters how good program you write but you don’t control all, some method might go weird and you will not have idea on that.

In real life if we plan for something then some problem we expect and some will be unexpected, normally we are ready for expected problem like going on outing and carrying medicine, first aid box,extra bed sheet etc, but there might be problem which we dint think of and it will be unexpected like vehicle breaks down completely, heavy rain in mid way or we lost some of our friends etc…

Java defines expected problem as “Checked” and unexpected one as “Unchecked”, these are the two types of exception java has.

For checked exception we write code to handle that as we expect that, something like carrying medicine in outing.

We don’t do anything for unchecked as we dint know this, something like natural disaster or vehicle break down.

How to handle checked exception:
Java provide try/catch block to handle exception, it basically tells complier that you writing some risky program and it might break. Compiler will not do anything in this case.It just knows that you doing something risky and you will handle it if something wrong happens.

“Life is short and we have to take risk but at the same time we should be ready to handle if something goes wrong, that’s what java says, you ‘try’ for risky things but be ready to ‘catch’ it”
try{
risky code goes here///
}catch(Exception ex){
}
All risky code you have to write inside try block, if something goes wrong it will be caught meaning control will come to catch block and it will tell what went wrong. Once there is any problem no other statement will be executed inside try block,
try{
Xxxxx – if exception came here rest lines will not be executed and control will go directly to catch section where it tells what the prob. is
Yyyyyy
Zzz
} catch (){}
There is another block called as finally, if you want your statement to get executed, regardless of whether there is an exception is in your program or not then you put that statement in finally block,
try{
xxxx/// if exception came here it will go to catch block
yyyyy
zzzzz
}catch(){
// some statement –this will not get executed until there is an exception in try block
{
finally {
// some statement … this statement will be executed even there is no exception in try block.
We make use of this for closing the DB connections, nullifying the collection object as we have to execute these statements regardless of exception.
}
Note:
1. Except System.exit(0), rest all statement will be executed written inside finally block.
2. Best thing is that, if try or catch block has return statement even though finally will execute.
3. We can have multiple no. of catch block along with finally block.
4. We should make sure subclasses should come before super classes in multiple catch blocks.
Throws and Throw clause:
If you don’t want to handle an exception you can tell your method to throw it and exception will be taken care by calling method.
Let’s see this in real life…. Suppose for outing you want to use Manikesh’s car and Manikesh’s car has got some problem so while giving the car Manikesh told you that car might give this problem.
Since you are going to use his car now it’s your responsibility to handle that expected problem, Manikesh just told and left, rest all is your job to do.
Same thing happens in java, you can declare your method like this

Public void fun() thorws xyzException

That’s it your job is done now whoever is going to use this method has to take care of this exception.

In some cases if you want to throw some user friendly exception, something which user can understand for such demand java has “throw” clause.
This will help you in throwing your own exception, user defined exception, like below.

Public void fun(){
Throw new Exception(“---“);
}

In above line I mentioned about some unexpected problems like nature disaster or vehicle breaks down, in java language we call it as run time exception, this is something compiler does not care about.
This also called as “Unchecked” exception, you can handle these exceptions as well but compiler really does not care about this.

Points to note:
1. A method can throw any number of exceptions.
2. Exceptions are polymorphic is nature.
3. You can declare exception using super type and can throw with sub types as well but vice versa is not possible.
Eg. public void fun() throws grandfatherException{
throw fatherException,
throw grandSonException
etc..
}
4. You can catch exception using super type but it’s advisable to catch specific exception.
5. Multiple catch blocks should be in ascending order, meaning subtype and then super type.
6. Making use of catch or finally block without try is impossible.
7. No code should be put between try and catch block.

No comments:

Post a Comment