Results 1 to 3 of 3
  1. #1
    scoobyrox is offline Member
    Join Date
    Aug 2010
    Posts
    13
    Rep Power
    0

    Default After catching the exception thrown

    Part of my school assignment is asking us to throw an exception if the answer is zero or less than zero. After catching the exception, I would like to re-ask the same question. I tried adding the same question after the catch statement but if I enter zero, it does not re-ask the same question.

    My code is below.

    try {

    System.out.print("Enter tutored time in minutes: ");
    matrix[i][0] = input.nextDouble();

    if (matrix[i][0] <= 0);
    throw new ArithmeticException("Minutes must be greater than zero");

    }
    catch (ArithmeticException ex) {
    System.out.println("Minutes must be greater than 0");
    }

    /* if I add the following statements here to re-ask the same question:
    **System.out.print("Enter tutored time in minutes: ");
    **matrix[i][0] = input.nextDouble();
    **it will not re-ask the same question if the answer is zero again */

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,334
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by scoobyrox View Post
    Part of my school assignment is asking us to throw an exception if the answer is zero or less than zero. After catching the exception, I would like to re-ask the same question. I tried adding the same question after the catch statement but if I enter zero, it does not re-ask the same question.
    Your code begs for a while loop; a while loop with a try-catch block in its body. If the loop ends a positive number was given by the user. If I were you I'd stick all that code in a separate method that only returns correct (positive) answers given by the user; it handles those try-catch blocks itself. A skeleton of the code could be:

    Java Code:
    int getAnswer() {
       int ans= -1;
       while (ans < 0) { //  as long as no correct answer given ...
          try {   
             ans= ...; // get answer from the user;
             if (ans < 0)
                throw ArithmeticException("incorrect anser:"+ans);
            return ans; // pass back a correct answer
          }
          catch (ArithmeticException ae) {
             System.out.println(ae.getMessage());
          }
       }
       return -1; // never reached
    }
    kind regards,

    Jos

  3. #3
    scoobyrox is offline Member
    Join Date
    Aug 2010
    Posts
    13
    Rep Power
    0

    Default Thanks.

    Quote Originally Posted by JosAH View Post
    Your code begs for a while loop; a while loop with a try-catch block in its body. If the loop ends a positive number was given by the user. If I were you I'd stick all that code in a separate method that only returns correct (positive) answers given by the user; it handles those try-catch blocks itself. A skeleton of the code could be:

    Java Code:
    int getAnswer() {
       int ans= -1;
       while (ans < 0) { //  as long as no correct answer given ...
          try {   
             ans= ...; // get answer from the user;
             if (ans < 0)
                throw ArithmeticException("incorrect anser:"+ans);
            return ans; // pass back a correct answer
          }
          catch (ArithmeticException ae) {
             System.out.println(ae.getMessage());
          }
       }
       return -1; // never reached
    }
    kind regards,

    Jos
    Thank you. That seems pretty simple. I think I am still not very good in creating algorithm before coding.

Similar Threads

  1. Replies: 2
    Last Post: 06-01-2010, 02:58 AM
  2. Catching exception from another library (class)
    By ezilka in forum New To Java
    Replies: 15
    Last Post: 05-26-2010, 06:59 AM
  3. How many no. of exceptions can be thrown????
    By Stephen Douglas in forum New To Java
    Replies: 8
    Last Post: 04-30-2010, 05:12 PM
  4. Replies: 2
    Last Post: 09-18-2009, 11:22 PM
  5. Which exception is thrown.....
    By money123 in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 03:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •