Using the throws Clause

In method quotient (Fig. 11.3, lines 9–13), line 10 is known as a throws clause. It specifies the exceptions the method might throw if problems occur. This clause, which must appear after the method’s parameter list and before the body, contains a comma-separated list of the exception types. Such exceptions may be thrown by statements in the method’s body or by methods called from there. We’ve added the throws clause to this application to indicate that this method might throw an ArithmeticException. Method quotient’s callers are thus informed that the method might throw an ArithmeticException. Some exception types, such as ArithmeticException, are not required to be listed in the throws clause. For those that are, the ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.