9.5 Triggering Custom Exceptions
So far, exceptions have only been caught, but not generated. In this section, we’ll show you how custom exceptions can be thrown. On one hand, exceptions may arise when the JVM is provoked, for example, in the case of an integer division by 0, or can be generated explicitly by using throw .
9.5.1 Triggering Exceptions via throw
If a method or a constructor itself is supposed to throw an exception, an exception object must first be created, and then, exception handling is triggered. In the lexicon, the throw keyword signals an exception, and processing stops at that point.
The exception type in the following example is IllegalArgumentException , which indicates an incorrect argument:
Player( int age
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access