Throwing Exceptions Using the throw Statement

Method main (Fig. 11.5) begins executing, enters its try block and immediately calls method throwException (line 10). Method throwException throws an Exception. The statement at line 26 is known as a throw statement—it’s executed to indicate that an exception has occurred. So far, you’ve caught only exceptions thrown by called methods. You can throw exceptions yourself by using the throw statement. Just as with exceptions thrown by the Java API’s methods, this indicates to client applications that an error has occurred. A throw statement specifies an object to be thrown. The operand of a throw can be of any class derived from class Throwable.

Software Engineering Observation 11.8

When toString ...

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.