Question: Run the following code: public class BadArray { public static void main(String[] args) { // Create an array with 3 elements. int[] numbers = {

Run the following code:

public class BadArray {

public static void main(String[] args) {

// Create an array with 3 elements.

int[] numbers = { 1, 2, 3 };

// Attempt to read beyond the bounds

// of the array.

for (int i = 0; i <= 3; i++)

System.out.println(numbers[i]);

}

}

1. What is the output of the program? If there is exception thrown, what is the name of the exception ( or Class name)? Is it Checked or Unchecked Exceptions?

2. Now write try and catch block in your code to handle exception. The ExceptionType in the catch block should be same exception you found in question# 1.

3. What other class name you can use as ExceptionType

try {

(try block statements...)

} Catch (ExceptionType ParameterName)

{ (catch block statements)

}

Sample Output:

1

2

3

You can't print more than 3 items

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!