The program illustrates the use of catching exceptions using try and catch.
- Exceptions are simply errors that can crash a program
- Try and catch are used to prevent errors from crashing a program
The required try and catch block is as follows:
<em>try {
</em>
<em> System.out.print(names[index]);
</em>
<em> }
</em>
<em> catch (ArrayIndexOutOfBoundsException e) {
</em>
<em> System.out.println(e.getMessage());
</em>
<em>}</em>
The flow of the above code segment is as follows:
First, we begin with the try block
<em>try {</em>
Then the program prints the element at the index
<em> System.out.print(names[index]); }</em>
If the index does not exist in the array (e.g. -1 or 11), then the catch block is executed
<em> catch (ArrayIndexOutOfBoundsException e) {</em>
This prints the appropriate exception
<em> System.out.println(e.getMessage());
}</em>
See attachment for sample run
Read more about similar programs at:
brainly.com/question/21330187