public class ExceptionTest1
{
int[] intArray = { 1 };
public ExceptionTest1()
{
// nothing needs to be done here
}
public static void main(String[] argv)
{
System.out.println("Main Called");
ExceptionTest1 etest = new ExceptionTest1();
etest.doMethodA();
}
public void doMethodA()
{
// something safe-no Exceptions thown here
System.out.println("Method A Called");
// call doMethodB here
doMethodB();
}
public void doMethodB()
{
System.out.println("Method B Called");
// something careless-access past end of array
for(int i = 0; i <= intArray.length; i++)
{
System.out.println("Index " + i +
" Value: " + intArray[i]);
}
System.out.println("Method B Done!");
}
}
Main Called Method A Called Method B Called Index 0 Value: 1 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at ExceptionTest1.doMethodB(Compiled Code) at ExceptionTest1.doMethodA(ExceptionTest1.java:22) at ExceptionTest1.main(ExceptionTest1.java:14)
--- Output Ends ---
- javac ExceptionTest1.java
- java ExceptionTest1