Question: Write a Java program named ExceptionLab which declares and initializes an integer array. Prompt the user for an array index and display the array element
Write a Java program named "ExceptionLab" which declares and initializes an integer array. Prompt the user for an array index and display the array element at that index. Insert the code which displays the element in a try/catch statement which catches an ArrayIndexOutOfBoundsException and prints the exception's stack trace when an exception is caught. Add a "finally" clause to your try/catch block displays "exiting program" regardless of whether an exception is caught or not. Run your program twice, once to verify that it behaves normally for a valid index and once to verify that it catches the exception and prints the stack trace for an invalid index. Attach and submit your .java file to this assignment. The following sample output demonstrates two runs of a program which meets these requirements:
ExceptionLab Enter an array index: 3 array[3] = 8 exiting program ExceptionLab Enter an array index: 10 java.lang.ArrayIndexOutOfBoundsException: 10 at ExceptionLab.main(ExceptionLab.java:19) exiting program
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
