Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

>>>>>//Design an exception class for index out of bound in an one-domensional ar

ID: 3659757 • Letter: #

Question

>>>>>//Design an exception class for index out of bound in an one-domensional array

public class IndexOutOfRange extends Exception
{
public IndexOutOfRange ( )
{
super ("array index out of bound");
}

public IndexOutOfRange (String msg)
{
super (msg);
}
}//Design a method to test the exception class above

public void testException ( ) throws IndexOutOfRange
{
int capacity = 25;
Integer [ ] x = new Integer [capacity];

for (int i = 0; i = capacity)
throws new IndexOutOfRange ( );
else
x[i] = num;
}
} >>>//Design an exception handler for the exceptio above


try
{

testException ( );

}
catch (IndexOutOfRange ex)
{




}

Explanation / Answer

public class IndexOutOfRange extends Exception
{
    public IndexOutOfRange()
    {
        super ("array index out of bound");
    }

    public IndexOutOfRange (String msg)
    {
        super (msg);
    }
}
public class TestApp
{
    public static void main(String[] args)
    {
        try
        {
            testException ( );
        }
        catch (IndexOutOfRange ex)
        {
            System.out.println("Index out of bounds custom exception");
        }
    }
   
    public static void testException() throws IndexOutOfRange
    {
        int capacity = 25;
        Integer [] x = new Integer [capacity];

        for (int i = 0; i <= capacity; i++)
        {
            if(i==25)
                throw new IndexOutOfRange();
            else
                x[i] = 10;
        }
           
    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote