Calling Time1 Method setTime with Invalid Values

To illustrate that method setTime validates its arguments, line 23 calls method setTime with invalid arguments of 99 for the hour, minute and second. This statement is placed in a try block (lines 21–24) in case setTime throws an IllegalArgumentException, which it will do since the arguments are all invalid. When this occurs, the exception is caught at lines 25–28, and line 27 displays the exception’s error message by calling its getMessage method. Line 31 outputs the time again in both formats to confirm that setTime did not change the time when invalid arguments were supplied.

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.