Q.
Write a program for multiple catch to fire ArrayIndexOutOfBoundsException and
StringIndexOutOfBoundsException both.
ANS::-
class MultiCatch
public static void main(String str[])
String a="Hello";
int b[]={1,2};
try
char c=a.charAt(10);
System.out.println(c);
for(int i=0;i<3;i++)
System.out.printf("%d\n",b[i]);
catch(StringIndexOutOfBoundsException e)
System.out.println(e);
catch(ArrayIndexOutOfBoundsException e)
System.out.println(e);
System.out.println("After Try-Catch Blocks");
}
Q.Write a program to handle the ArithmeticException.
class Example1
public static void main(String args[])
try{
int num1=30, num2=0;
int output=num1/num2;
System.out.println ("Result: "+output);
catch(ArithmeticException e){
System.out.println ("You Shouldn't divide a number by zero");
Q. Define an object reference and initialize it to null. Try to call a method through
this reference. Now wrap the code in a try-catch clause to catch the exception.
class NULL
public static void main(String str[])
NULL ob=null;
try
ob.go();
catch(Exception e)
System.out.println(e);
System.out.println("NORMAL FLOW....");
System.out.println("Rest Of The Code.....");
}
void go()
System.out.println("HELLO WORLD");
Q.Write a program to fire the NegativeArraySize exception.
class Negative
public static void main(String str[])
int[] arr=new int[-10];