EXCEPTION HANDLING
Stuff happens like file not is not there , memory not
available , server is down etc. No matter how good a programmer we are but we
need a code to handle these type of exceptional situations.
Exception is an abnormal condition which occurs in a
piece of a code during the execution of the program. Exception is an instance
of classes derived from the Throwable class. When an exceptional condition
arises , an object representing that exception is created and thrown at the
method that caused the error. The method may choose to handle the exception
itself or pass it on.
Exception can be generated by java runtime system or
can be manually generated by our code.
Java provides 5 keywords to handle exception:
1) try
: Program statements that
we want to monitor for exception are placed in it. If exception occurs within
try block, it is thrown.
try
{
// block of
code to monitor for errors
}
2) catch
: It is used to catch the exception generated and handle it in
some rational manner. We can put multiple catch statements.
catch(ExceptionType1 exObject)
{
//exception
handler for ExceptionType1
}
catch(ExceptionType2 exObject)
{
//exception
handler for ExceptionType2
}
3) finally
: Any code that must be executed before a method returns , is
put in a finally block.
finally
{
//put the
code to be executed before the control returns from the method
//the code
in this block executes whether the exception occurs or not
}
4) throw
: It is used to throw an exception manually.
throw instance
of Throwable class ;
5) throws
: Any exception that is thrown out of a method must be specified by a throws. It is used to
specify the list of exception which might be generated by the method so that
the caller of the method can protect against them.
return_type method_name (list of parameters) throws (list of Exceptions separated by
‘ , ‘ )
{
//body
}
A simple program :
class excp
{
public
static void main(String ar[])
{
int
d,a;
try{
d=0;
a=42/d;
// exception occur in this line so the control is transferred to catch block
System.out.println("This
will not be printed");
}
catch(ArithmeticException
e)
{
System.out.println("exceptional
situation : divide by zero");
}
}
}
Output
: exceptional situation : divide by zero
In
this program we are dividing 42 by 0 (zero). So to handle this exception we
used the catch clause .
The
scope of the catch clause is restricted to those statements, specified by the
immediately preceding try statement .
Catch
block must start just after the closing of the try block. If a try block has
multiple catch blocks then they they must placed one after other.
The
goal of catch clause is to resolve the exception condition and then continue on
as if the error had never happened.
Displaying
a description of an exception
Throwable
overrides the toString() method so that it returns a string containing a
description of the exception.
We
can display this description in a println() statement by simply passing the
exception as an argument.
For
example :
catch(ArithmeticException e)
{
System.out.println("exception
: " + e);
}
Now each divide by zero exception will display this
message-
Exception: java.lang.ArithmeticException : / by zero
It
is useful in case of debugging.
No comments:
Post a Comment