Let us revisit the email management system classes you designed in question dc38.
Suppose that two distinct email addresses with the same string identifier should never be created. Adapt your email address classes so that a set of all previously used identifiers is stored, and an exception is thrown if an email address is created using a previously encountered identifier. Suppose that this problem is so important that clients of the email address classes should be forced to catch the associated exception if it is ever thrown. When printed, your new exception should display the name of the duplicate identifier that caused the problem.
Write a demonstration program that causes your new exception to be thrown, catches it, and displays an appropriate message.
Advanced: Suppose that the hierarchy of email addresses arising from nested email groups should never contain
cycles. Adapt the method used to add email addresses to a group (in the sample solution this is the addEmailAddress()
method of
GroupEmailAddress
) so that if the new addition would introduce a cycle, an appropriate exception is thrown. Decide whether
this exception should be a runtime exception or a caught exception, and explain your answer.