Source code for ExceptionTest1.java:

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!");
	}
}

Output from ExceptionTest1.java:

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 ---

NOTE:

You are reading previously generated output. You are not currently running the ExceptionTest1 application at the momement. You need to compile and run the source code first.

To run this program:


Authors: Kevin Chu and Eric Brower
Copyright 2000 Prentice Hall PTR