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=[Link](10);
[Link](c);
for(int i=0;i<3;i++)
[Link]("%d\n",b[i]);
catch(StringIndexOutOfBoundsException e)
[Link](e);
catch(ArrayIndexOutOfBoundsException e)
[Link](e);
[Link]("After Try-Catch Blocks");
}
[Link] a program to handle the ArithmeticException.
class Example1
public static void main(String args[])
try{
int num1=30, num2=0;
int output=num1/num2;
[Link] ("Result: "+output);
catch(ArithmeticException e){
[Link] ("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
[Link]();
catch(Exception e)
[Link](e);
[Link]("NORMAL FLOW....");
[Link]("Rest Of The Code.....");
}
void go()
[Link]("HELLO WORLD");
[Link] a program to fire the NegativeArraySize exception.
class Negative
public static void main(String str[])
int[] arr=new int[-10];