Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Exception Handling

  1. #1

    Default Exception Handling

    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.


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Exception Handling

    Hello.
    You need to know how JVM handles and processes exceptions.
    I advice you to refer the textbook "Java How to Program" by "Deitel & Deitel". It has an exclusive and dedicated chapter on "Exception Handling".
    If you read that chapter all your doubts shall be cleared.

    Syed.

  3. #3

    Default Re: Exception Handling

    Thanks. I have it, 9th edition. Exception handling is covered in all of Chapter 11, 25 pages worth. Assuming I have read it, of course assuming, because why would I ask a question when I have the resource to find it myself? Simple explanation, I have read it, thus why I am asking the afore-mentioned question.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,051
    My Mood
    Mellow
    Thanks
    184
    Thanked 650 Times in 637 Posts

    Default Re: Exception Handling

    The throw statement doesn't need a reference. The throw statement is a safety valve that "blows" if the setTime() method can't accomplish its mission, and it (the throw) sends the message to the calling program to let it know there was a problem. The calling program's catch clause is there to detect that the safety valve was opened and receives the message sent by the throws statement that explains why the safety valve had to be opened. The "throw" is like a special return statement that wasn't specified by the setTime()'s method signature, but it could have been by declaring it thusly:

    public void setTime( int h, int m, int s ) throws IllegalArgumentException {}

    It's hard to get my head around your confusion to provide a direct answer to your question, so I hope this helps. It's the best I can do.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    LinuxPower (September 6th, 2013)

  6. #5

    Default Re: Exception Handling

    Thanks Greg. I think I may have to just keep reading this chapter until it finally clicks, and keep reading the codes that I have. It's hard for me to explain exactly what I mean, but I appreciate all of your help(s). Thanks again all!

  7. #6
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Exception Handling

    The throw statement itself is a reference to any catch while running back up the program. It is similar to the process that occurs when the if statement knows if it is false to go directly to the else.

  8. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Exception Handling

    Quote Originally Posted by LinuxPower View Post
    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?
    This line of code assigns the given text to, and throws the exception:
    throw new IllegalArgumentException( "hour, minute, and/or second was out of range" );

    This line of code catches exception, named by the variable e
    catch ( IllegalArgumentException e ) {

    This line of code gets the text with e.getMessage()
    System.out.printf( "Exception: %s\n\n", e.getMessage() );

Similar Threads

  1. Exception Handling
    By LinuxPower in forum Java Theory & Questions
    Replies: 4
    Last Post: September 2nd, 2013, 10:10 PM
  2. [SOLVED] exception handling
    By Topflyt in forum Exceptions
    Replies: 5
    Last Post: December 22nd, 2011, 11:00 AM
  3. [SOLVED] Exception Handling
    By Melawe in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 21st, 2011, 06:39 PM
  4. Exception handling
    By JavaGirl9001 in forum Object Oriented Programming
    Replies: 1
    Last Post: November 26th, 2011, 07:45 PM
  5. Exception handling
    By AnithaBabu1 in forum Exceptions
    Replies: 6
    Last Post: August 27th, 2008, 09:37 AM