11.13 User-Defined Exceptions
There will be times when we want to design our own exception class because the predefined Java exception classes do not fit our needs.
Suppose we are interested in designing a class encapsulating email addresses. We will call that class EmailAddress. To keep things simple, we will say that a legal email address is a String containing the @ character. In order to prevent instantiation of objects with illegal email addresses, we will design our EmailAddress constructor so that it throws an exception if its argument, a String, does not contain the @ character.
To do that, we first design an exception class that encapsulates an illegal email exception. We call our class IllegalEmailException and we will throw an exception ...
Get Java Illuminated, 5th 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.