import java.util.*; class A { void f1() { try { String v="hello"; char c=v.charAt(0); int a[]=new int[5]; a[15]=20; //correct statement is a[1]=20; System.out.println("a[15]="+a[15]); //correct statement is System.out.println("a[1]="+a[1]); } catch(ArrayIndexOutOfBoundsException a) { System.out.println("array index is outof range"); } catch(StringIndexOutOfBoundsException p) { System.out.println("String index is outof range"); } } void f2() { try { int x=10,y=0,z;// correct statement is int x=10,y=2,z; z=x/y; System.out.println("z="+z); } catch(ArithmeticException a) { System.out.println("Denominator should not be zero"); } catch(InputMismatchException i) { System.out.println("please enter integer values only"); } } } class B { void f4(String s1,String s2) { try { int f1=Integer.parseInt(s1); int f2=Integer.parseInt(s2); int f3=f1+f2; System.out.println("f3="+f3); } catch(Exception e) { System.out.println(e); } } } public class Main { public static void main(String args[]) { A a=new A(); B b=new B(); a.f1(); a.f2(); b.f4("ten","20"); //correct statement is dm.f4("10","20"); } }
array index is outof range
import java.util.*; class A { void f1() throws ArrayIndexOutOfBoundsException,StringIndexOutOfBoundsException { String v="hello"; char c=v.charAt(0); int a[]=new int[5]; a[15]=20; //correct statement is a[1]=20; System.out.println("a[15]="+a[15]); //correct statement is System.out.println("a[1]="+a[1]); } void f2() throws ArithmeticException,InputMismatchException { int x=10,y=0,z;// correct statement is int x=10,y=2,z; z=x/y; System.out.println("z="+z); } } class B { void f4(String s1,String s2) throws Exception { int f1=Integer.parseInt(s1); int f2=Integer.parseInt(s2); int f3=f1+f2; System.out.println("f3="+f3); } } public class Main { public static void main(String args[]) { A a=new A(); B b=new B(); try { a.f1(); a.f2(); b.f4("ten","20"); //correct statement is dm.f4("10","20"); } catch(StringIndexOutOfBoundsException se) { System.out.println("String index is outof range"); } catch(ArrayIndexOutOfBoundsException ee) { System.out.println("array index is outof range"); } catch(ArithmeticException ae) { System.out.println("Denominator should not be zero"); } catch(NumberFormatException ee) { System.out.println("please enter integer values only"); } catch(Exception e) { System.out.println(e); } } }
array index is outof range