Manu

Saturday, November 19, 2016

Write a program using try-catch-finally and throw to handle NoSuchMethodException, ArrayIndexOutofBoundsException

CODE:
import java.util.ArrayList;
import java.util.List;
import java.lang.reflect.Method;

public class ExceptionTest
{
    public ExceptionTest()
    { }
   
    private void testNoSuchMethodException() throws NoSuchMethodException
    {
        Class c;
        try
        {
            c=Class.forName("java.lang.String");
            try
            {
                Class[] paramType=new Class[5];
                Method m=c.getDeclaredMethod("fooMethod",paramType);
            }   
            catch(SecurityException e)
            {
                e.printStackTrace();
            }   
            catch(NoSuchMethodException e)
            {
                e.printStackTrace();
                throw e;
            }
        }
        catch(ClassNotFoundException e)
        {
            e.printStackTrace();
        }
    }
   

private void testArrayOutOfBoundException()
    {
        List nameList=new ArrayList();
        nameList.add("Harshkumar");
        try
        {
            nameList.get(1);
        }
        catch(ArrayIndexOutOfBoundsException ex)
        {
            ex.printStackTrace();
        }
    }
    public static void main(String args[])
    {
        ExceptionTest obj=new ExceptionTest();
        try
        {
            obj.testNoSuchMethodException();
        }
        catch(NoSuchMethodException e)
        { }
        obj.testArrayOutOfBoundException();
    }
}

OUTPUT:


No comments:

Post a Comment