/* Program to handle
ArithmeticException */
class
Exception1
{
public static void main(String args[])
{
int a,b,c;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
try
{
c = a / b;
System.out.println("Result is :
" + c);
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic
Exception Caught");
System.out.println("Division by zero
is not possible");
}//catch
}//main
}//class
->
In the above program, if the ArithmeticException is raised, it is handled by
the corresponding catch block.
NOTE: A
single try block may generate multiple exceptions. Hence, to handle each Exception-type,
we have to define separate catch blocks. It is important to note that a try block
can have multiple catch blocks. But, a catch block must associate only one try
block.
->The
following program illustrates a try block with multiple catch blocks.
/* Program to define multiple catch
blocks under a try block */
class
Exception2
{
public static void main(String args[])
{
int a[]={10,20,30,40,50};
try{
a[5]=a[2] / (a[1]-20);
System.out.println("result= " +
a[5]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
catch(ArithmeticException e)
{
System.out.println(e);
}
}//main
}//class
->In
the above program, if a try block is defined to generate multiple exceptions,
which exception object is created first, its corresponding catch block will be
executed. Then, the program will be terminated.
->Suppose
that if a try block is defined to generate more number of exceptions, defining
corresponding catch blocks will increase the program size. To avoid this
problem, to handle any exception type, we can define a single catch block where
we define the reference of the super class of Exception classes “Exception
class”.
->The
following program demonstrates this concept:
/* Program to define a catch block
that handles multiple Exceptions */
class
Exception3
{
void fun1()
{
int a[]={10,20,30,40,50};
try{
a[5]=a[2] / (a[1]-20);
System.out.println("result= " +
a[5]);
}
catch(Exception e)
{
System.out.println(e);
}
}// end of fun1()
public static void main(String args[])
{
Exception3 e1 = new Exception3();
E1.fun1();
}//main
}//class
->In
the above program,the try block in fun1() is defined to generate
ArithmeticException and ArrayIndexOutOfBoundsException. But, the first
exception that will raise is ArithmeticException so the corresponding catch
block will gets executed.
“throws” keyword :
->
Generally if a function is defined to generate an exception, we have to handle
the exception in that function only. Suppose assume that a function does not
wants to handle exceptions inside it. Then, they can pass the exception class
objects to the functions which call them. This can be done by using a special
keyword called “throws”. If a function is defined to throw an exception, then
the calling function must handle that exception inside it.
->
The following program demonstrates the use of “throws” keyword.
/* Program to demonstrates throws
keyword */
class
Sample
{
void fun(int x,int y) throws
ArithmeticException
{
int a,b,c;
a = x;
b = y;
c = a / b;
System.out.println(" c= " + c);
}
}
class
ThrowDemo
{
public static void main(String args[])
{
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
Sample s = new Sample();
try {
s.fun(10,0);
}
catch(ArithmeticException e)
{
System.out.println(e);
}
}// end main
}
// end class
->In
the above program, fun1() is defined to throw ArithmeticException. Hence,
main() method is handling it within its try-catch block. If main() method also
doesn’t want to handle that exception, we can define “throws” with main(). Then
that object is passed to JVM and the program is terminated abnormally.