Exception
Handling
PROGRAM:-
class
Exceptiondemo
{
public static void main(String args[])
{
int a=0,b=10,c;
try
{
c=b/a;
System.out.println("c value
is:"+c);
}
catch(ArithmeticException e)
{
System.out.println(e);
}
finally
{
System.out.println("finally block is
executed");
}
}
}
File Name: -
Exceptiondemo.java
Commands:-
javac Exceptiondemo.java
java Exceptiondemo
OUTPUT:-
java.lang.ArithmeticException:
/ by zero
finally block is executed
PROGRAM:-
class
Exceptiondemo1
{
public static void main(String args[])
{
int a[]={10,20};
try
{
System.out.println(a[3]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
}
}
File Name: -
Exceptiondemo1.java
Commands:-
javac Exceptiondemo1.java
java Exceptiondemo1
OUTPUT:-
java.lang.ArrayIndexOutOfBoundsException:3