Hi all, I have another question about exception handling, as stated previously, it's a bit confusing, although you all have helped clear it up better for me. Anyways, here is my code, and then I'll ask the question:
public class Time1 {
private int hour;
private int minute;
private int second;
public void setTime( int h, int m, int s ){
if( ( h >= 0 && h < 24 ) && ( m >= 0 && m < 60 ) && ( s >= 0 && s < 60 ) ) {
hour = h;
minute = m;
second = s;
}//end if
else {
throw new IllegalArgumentException( "hour, minute, and/or second was out of range" );
}//end else
}//end setTime method
public String toUniversalString() {
return String.format( "%02d:%02d:%02d", hour, minute, second );
}//end toUniversalString method
public String toString() {
return String.format( "%d:%02d:%02d %s", ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ),
minute, second, ( hour < 12 ? "AM" : "PM" ) );
}//end toString method
}//end Time1 class
public class Time1Test {
public static void main(String[] args) {
Time1 time = new Time1();
System.out.printf( "The initial universal time is: " );
System.out.println( time.toUniversalString() );
System.out.print( "The initial standard time is: " );
System.out.println( time.toString() );
System.out.println();
time.setTime( 13, 27, 6 );
System.out.printf( "Universal time after setTime is: " );
System.out.println( time.toUniversalString() );
System.out.print( "Standard time after setTime is: " );
System.out.println( time.toString() );
System.out.println();
try {
time.setTime( 99, 99, 99 );
}//end try
catch ( IllegalArgumentException e ) {
System.out.printf( "Exception: %s\n\n", e.getMessage() );
}//end catch
System.out.println( "After attempting invalid settings: " );
System.out.print( "Universal time: " );
System.out.println( time.toUniversalString() );
System.out.print( "Standard time: " );
System.out.println( time.toString() );
}//end main
}//end Time1Test class
---------------------------------------------------------------END CODE--------------------------------------------------------------------------
My question is this: How does the "catch" statement in the Time1Test class know to print out the message that was defined from the Time1 class? I don't understand because the "throw" statement has no reference, so how does the Time1Test class know to print what that code says, rather than what the system would have printed out? I'm so confused.