|
|
|
|||
|
Specifying the Exceptions Thrown by a MethodThe previous section showed you how to write an exception handler for thewriteList method in the ListOfNumbers class.
Sometimes, it's appropriate for your code to catch exceptions that
can occur within it. In other cases, however, it's better to let a
method further up the call stack handle the exception. For example,
if you were providing the ListOfNumbers class as part of a package
of classes, you probably couldn't anticipate the needs of all of the
users of your package. In this case, it's better to not
catch the exception and to allow someone further up the call stack to
handle it.
If the
As you recall, the new FileWriter("OutFile.txt") statement
might throw an IOException (which is not a runtime exception).
The victor.elementAt(i) statement can throw an
ArrayIndexOutofBoundsException (which, as a subclass of RuntimeException, is
a runtime exception).
To specify that
Remember that ArrayIndexOutofBoundsException is a runtime exception, so you
don't have to specify it in the throws clause, although you can.
| |||
|