Java Reference
In-Depth Information
14. Assuming the String variable river holds the value
ÐMississippiÑ , what is the value of river.substring(1,
2)? Of river.substring(2, river.length() - 3)?
P RODUCTIVITY H INT 4.2: Reading Exception Reports
You will often have programs that terminate and display an error message, such as
Exception in thread "main"
java.lang.StringIndexOutOfBoundsException:
String index out of range: -4
at java.lang.String.substring
(String.java:1444)
at Homework1.main(Homework1.java:16)
An amazing number of students simply give up at that point, saying Ȓit didn't
workȓ, or Ȓmy program diedȓ, without ever reading the error message. Admittedly,
the format of the exception report is not very friendly. But it is actually easy to
decipher it.
When you have a close look at the error message, you will notice two pieces of
useful information:
1. The name of the exception, such as
StringIndexOutOfBoundsException
2. The line number of the code that contained the statement that caused the
exception, such as Homework1.java:16
The name of the exception is always in the first line of the report, and it ends in
Exception . If you get a StringIndexOutOfBoundsException , then
there was a problem with accessing an invalid position in a string. That is useful
information.
The line number of the offending code is a little harder to determine. The
exception report co ntains the entire stack traceȌthat is, the names of all methods
that were pending when the exception hit. The first line of the stack trace is the
method that actually generated the exception. The last line of the stack trace is a
line in main . Often, the exception was thrown by a method that is in the standard
160
161
Search WWH ::




Custom Search