Exception Handling
Exceptions are those errors
which can’t be resolved at compile time and often comes to occur at runtime
(like divide by zero, file not found or input output failure) these all are
runtime error and so called exception.
Dealing with Exception
In JAVA we have to put some
extra codes in our program which will take care of these exceptions.
So as matter of fact JAVA
provides us a class Hierarchy which provide us so many types of Exception
class.
Types of Exception:--
1. Unchecked
Exception
ü Arithmetic
Exception
ü ArrayIndexOutOfBoundsException
ü NullPointerException
ü ClasscastException
ü NumberFormateException
2. Checked
Exception
ü IOException
ü InterruptedException
These are some types of exceptions which is commonly used in
learning exception Handling.
Exception Handling is
a process in which you can save your program from corrupted or failure when
some exception occurs.
Keywords used in
Exception Handling
ü try
ü catch
ü throws
ü throw
ü finally
try :-- It is block in program which surrounds those
statements who are in exception zone.
catch :-- This is also a block where we can write some code
to handle the exception because as soon as any exception occurs in try block
execution control jumps to the matching catch block.
thorws :-- It makes the list of all exception which may occur
in any method.
thorw :-- This keyword is used to throw the exception
explicitly and some where later in the program it is catch by some catch block.
finally :-- finally block is always optional you can use or
may not use and it is catch independent (means no matter exception occurs or
not ) finally block always get executed.
Example of try and catch:----
public class abc
{
public
static void main(String as[])
{
int
a=12;
int
b=0;
try
{
int
c=a/b;
}catch(ArithmeticException
e)
{
System.out.println("Exception
occured "+e);
}
}
}
Multiple catch for try:----
public class abc
{
public
static void main(String as[])
{
int
a=12;
int
b=0;
int
arr[]=new int[3];
try
{
arr[5]=a;
int
c=a/b;
}catch(ArrayIndexOutOfBoundsException
ar)
{
System.out.println("Exception
occurred "+ar);
}
catch(ArithmeticException
er)
{
System.out.println("Exception
occurred "+er);
}
catch(Exception
e)
{
System.out.println("Exception
occurred "+e);
}
}
}
Note :-- whenever any exception is occurred all catch is
matched one by one from top to bottom until any catch match the exception type.
If no catch is matched to exception java runtime automatically handle the
exception.
Note:-- in above example we have putted three catch block or
we can also put only on catch of Exception class that will also work because ArrayIndexOutOfBoundsException
and ArithmeticException are the sub class of Exception class.
Then
the fact is when you use multiple catch block for single try block always keep
your approach specific to general.
Exception --à is a
general
ArithmeticException -àis specific
to Arithmetic type Exception only
Use of throw throws and finally
Throws and
throw is used when we have method except main(). Writing so many try and catch
block in all method is never a good idea so you can just throw the exception
from a method and it can be cached elsewhere in program or at the time when
method gets invoked.
class test
{
void
divide()throws ArithmeticException
{
int
a=34;
int
b=0;
int
c=a/b;
throw
new ArithmeticException();
}
void
array()throws ArrayIndexOutOfBoundsException
{
int
a=34;
int
b=0;
int
c=a/b;
throw
new ArrayIndexOutOfBoundsException();
}
}
public class abc
{
public
static void main(String as[])
{
test
t = new test();
try
{
t.divide();
t.array();
}
catch(Exception
e)
{
System.out.println("Exception
occurred "+e);
}
finally
{
System.out.println("this
is finally block it's always run");
}
}
}
Click here for Custom Exception…………….
No comments:
Post a Comment