>>>>>//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 arraypublic 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;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.