Yes.
Here is a run of the program.
The run encounters an ArrayIndexOutOfBoundsException
.
The catch
block uses getMessage()
to print the message in the exception,
and then uses printStackTrace()
.
C:\chap81>java IndexPractice Enter the data:8 Enter the array index:10 This is your problem: 10 Here is where it happened: java.lang.ArrayIndexOutOfBoundsException: 10 at IndexPractice.main(IndexPractice.java:18) Good-by
Here is another run of the program,
this time with an InputMismatchException
.
The InputMismatchException
did not contain a message,
so its getMessage()
method returned a null string.
C:\chap81>java IndexPractice Enter the data:rats This is your problem: null Here is where it happened: java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at IndexPractice.main(IndexPractice.java:15) Good-by
A more sensible program would use a loop to collect data from the user and place it in the array. Exceptions would cause the program to repeat the prompt. This would be much more user-friendly than halting. If a user has entered 999 data items, it would not be friendly to halt the program if item 1000 is wrong!
What does the "15" mean in
at IndexPractice.main(IndexPractice.java:15)