Throws Keyword Program

without throws keyword

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");
    }
}

Output:

array index is outof range


with throws Keyword Program

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);
        }
    }
}

Output:

array index is outof range


Exception Handling In Java



More Questions


168 . Write a Throws Keyword Program
169 . Write a Finally block Program
170 . Write a program to handle negative age exception
171 . Write a program to handle negative salary exception
172 . Write a Simple interest program to handle negative amount and duration and interest using user defined exception
173 . Write a String length program
174 . Write a string charAt() method program
175 . Write a string toLowerCase() Program
176 . Write a String toUpperCase() program
177 . Write a string concat() program
178 . Write a string compareTo program