Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Scenario You are a software developer and your client uses a program that stores the odd values in an array. You know that using arrays
Scenario
You are a software developer and your client uses a program that stores the odd values in an array. You know that using arrays may also create an exception error when trying to access an index of an array when that index is out of bounds. In this exercise, the program will request an array size from the user, as well as the values, and an index. Using the try and catch block to handle array index out of bounds exception handling that may occur.
Instructions
In this exercise you will use try and catch blocks to handle an array out of bounds error ArrayOutOfBoundsException from crashing the program.
An example of the program is shown below:
Enter the size of the array
Enter integers
Enter an index to retrieve
java.lang.ArrayIndexOutOfBoundsException: Index out of bounds for length import java.util.Scanner;
public class BonusBug
public static void mainString args
Scanner scanner new ScannerSystemin;
System.out.printEnter the size of the array ;
int size scanner.nextInt;
int array new intsize;
System.out.printlnEnter size integers ;
for int i ; i size; i
arrayi scanner.nextInt;
System.out.printEnter an index to retrieve ;
int index scanner.nextInt;
try
int value arrayindex;
System.out.printlnValue at index index is: value;
catch ArrayIndexOutOfBoundsException e
System.out.printlnArray index out of bounds error: egetMessage;
scanner.close;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started