Wednesday 16 May 2012

Java Program to rethrow an exception

Write a program to rethrow an exception – Define methods one() & two(). Method two() should initially throw an exception. Method one() should call two(), catch the exception and rethrow it Call one() from main() and catch the rethrown exception.


class MyThrow
{
    void two(int x)
    {   

        try
        {
            int b = 234/x;
            System.out.println("b = "+b);
        }
        catch(ArithmeticException e)
        {
            System.out.println("catch first time");
            throw e;      //thtow is just like a return statement
        }

    }
    void one(int a)
        {
            int x=a;
           
           
                two(a);
           
        }

}


class Ass59
{
    public static void main(String args[])
    {
        MyThrow t = new MyThrow();
        try
        {
            t.one(args.length);
        }
        catch(ArithmeticException e)
        {
            System.out.println("catch second time");
        }
       
        System.out.println("Bye");
    }
}

0 comments:

Post a Comment

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel