1. Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com
Exception handling
Exception: are those run time error which can be managed programmatically. The process of
managing error in application is called exception handling.
The purpose of exception handling is to avoid the abnormal program termination.
In java each run time error is represented as an object. family of classes is provided bu sun micro
system to represent run time error. Top most member of the family is a class named throwable
.it has two subclasses.
Commonly used subclasses of Exception:
1. Arithmetic Exception:
Represent invalid operations. Such as division by zero.
2. ArrayIndexoutofBound Exception:
Attempt to refer non existence array element.
3. Number format exception:
Represents an attempt to convert nonnumeric string in to a number.
4. Null pointer exception:
THROWABLE
EXCEPTION
Can b e managed
programmatically
ERROR
(Can not be
managed
programmatically
2. Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com
It represents an attempt to use a null containing reference variable for referencing object
members.
Commonly used subclass of Error:
1. Virtual machine error:
Represents malfunctioning of JRE.
2. Stack Overflow Error:
Represents lack of space in stack.
3. No Class definition found Error:
Represents inability to locate a referenced class.
4. No such Method error:
Represents inability to locate a referenced method etc.
Following keyword provided by Java to facilitate exception handling:
Try
Catch
Throw
Throws
Finally
Try keyword:try keyword is used to make a block of statement as error prone.try keyword is
never used alone .it is used with either catch or finally or both.
Catch: catch keyword is used to define a error handler block.it contains statement which are to be
executed when a run time error occurs.
Finally:finally keyword is used to define a block of statement which are to be executed before
normal or abnormal termination.
Try
{
Error prone statement
}
Catch(Exception object)
{
Error handling statement
}
Try
{
;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;
}
Finally
{
Statement which are to be
executed before termination of
Try
{[
;;;;;;;;;;;;;;;;;;;;
‘’’’’’’’’’’’’’’’’’’’’
;;;;;;;;;;;;;;;;;;;
}
Catch(Exception object)
{
;;;;;;;;;;;;;;;;;;;
3. Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com
program
}
;;;;;;;;;;;;;;;;;;;;;;
}
Finally
{
;;;;;;;;;;;;;;;;;;;;;;;
‘;;;;;;;;;;;;;;;;;;;;;;;;
}
Note:more than one catch block can be associated to a try block.but only one finally block can
be used.
Ex;:two number are received as command line argument first no is divided by second number
and result is displayed on console.
Class Divide
{
Public static void main(String arr[])
{
Try
{
Int a=Integer.parseInt(arr[0]);
Int b=Integer.parseInt(arr[1]);
Int c =a/b;
S.O.P(“Result=”+c);
}
Catch(ArithmaticException e)
{
S.O.P(“second number must be non zero”);
4. Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com
}
Catch(ArrayIndexoutofBoundException e)
{
S.O.P(“two argument must be provided”);
}
Catch(NumberFormatException e)
{
S.O.P(“argument must be numeric”);
}
}
}
Ex:Exception generated in method and caught in another.
Class A
{
Void getException()
{
Int [] num=new int[4];
S.O.P(“before Execution of exception”);
Num[7]=4;
S.O.P(“cannot display”);
}
}
Class Test
{
p.s.v.m(String arr[])
5. Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com
{
Try
{
A.getException();
}catch(ArrayIndexOutOfBoundsException ex)
{
S.O.P(“index is out of Bound”);
}
S.O.P(“after the Exception caught”);
}
}
Block diagram of generalized exceptions:
Only exit from try block resulting an exception can transfer control to catch block. A catch
block can only catch the thrown exception.
Throw: throw keyword is used for throwing exception explicitly. it is used for following:
For throwing user defined exception.
For customizing the description of predefined exception.
For re throwing exceptions.
6. Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com
Ex. Program below show the use of throw. Exception is thrown from the point where it is
generated.
Class Divide
{
public static void main(String arr[])
{
Try
{
If(arr.length<2)
{
throw(new Exception (“two argument must be provided”);
int a=Integer.parseInt(arr[0]);
int b = Integer.parseInt(arr[1]);
if(b==0)
throw(new Exception(“second argument should be non zero”);
int c=a/b;
S.O.P(“Result=”+c);
}
Catch(Exception e)
{
S.O.P (e);
}
}
7. Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com
Finally Block: if the try block is executed, then finally block is guaranteed to be executed
regardless of whether any catch block execute or not. Since finally block is always executed
before control transfer to its final destination. It can be used to specify any clean up code.
Example :
Class Average
{
Public static void main(string arr[])
{
printAvg(100,0);
S.O.P(“exit”);
}
Public static void printAvg( int sum,int num)
{
Try
{
Int avg=CAvg(sum,num);
S.O.P(“Sum avg=”+sum+”/”+num+”=”+avg);
}
Catch(Arithmeticexception e)
{
e.printStackTrace();
S.O.P(“exception handled in printAvg”);
}
Finally
{
S.O.P(“finally done”);
8. Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com
}
Throws:
If any method creates exception that it does not handle. So it specify a behavior so that caller of
the method can guard themselves against that exception. So we can include throws in the method
declaration.
A throws clause list type of exception that a method might throw.
E.g
Type methodname(Parameter) throws Exception-list
{
}
In exception list you can use comma to separate the exception types:
Class ThrowsDemo
{
Static void throwOne ()throws illegalAccessException
{
S.O.P(“inside throw one”);
Throw new illegalAccessException (“demo”);}
Public static void main(String arr[])
{
Try
{
throwOne();
}catch(illegalAccessException e)
{
9. Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com
S.O.P(“caught “+e);
}
}
}
Difference between throw and throws:
Both can be used together in a class but their behavior is different.
Throws is used to indicate or enforce the caller method to handle that checked exception or
caller method can also write throws clause to propagate the exception handling responsibility to
its caller method.
Throw is used to write throw statement which throws the exceptions
JVM can throw exception while executing the program.