• Post Reply Bookmark Topic Watch Topic
  • New Topic

Newb! Help with try/catch  RSS feed

 
Dawn Goxhaj
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so I am doing a fraction exception test with a denominator is zero exception. everything compiles except the actual FractionExceptionTest which gives me one error in the catch which is: error: exception DenominatorIsZero is never thrown in body of corresponding try statement
            catch(DenominatorIsZero e)
            ^
1 error

I have found a plethora of reasons for this but am falling victim to information overload and am getting nowhere. Below are all 3 parts of the code.  hoping someone can help. 

Thanks in advance!!
MJ



 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch, Dawn!

We can't really help you unless you post the full error message, and your Fraction class.
 
Dawn Goxhaj
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops.  sorry about that! 
here's my fraction class:

 
Dawn Goxhaj
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
geez.  sorry again for messing up.  guess I've become too frustrated with this :/




 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Your test uses the Fraction class. You showed us the code for the Fraction2 class.

Henry
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few other remarks about your code:

  • You're declaring variables before they're being used. you should declare variables in the deepest scope possible, and initialize them in one go.
  • You don't have to pay per character. Write out identifiers: numerator instead of num, getNumerator() instead of genN().
  • Your test class' loop body will never run.
  • Dividing by zero is a bug, which should not be recoverable. DenominatorIsZero should be a RuntimeException.
  • Why do denominators have to be greater than zero? Isn't -2 a valid denominator?
  • Why do you provide a default fraction of 1/1?
  • Why does your fraction constructor not perform parameter checking?
  • Why does your fraction class have mutators?
  • Don't overload the equals() method. Either override the equals(Object) method, or give the method a different name.
  •  
    Dawn Goxhaj
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    well that solved that problem.  I made so many versions trying different things I forgot to change Fraction to Fraction2.  So big thanks for that.  I always seem to miss the simple things.

    but then a new problem popped it's ugly head out.  It's not letting me input the num and den.  As you can see I do have the scanner s....but it's not giving me the option to add input. 
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7987
    143
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That issue is raised in my third bullet point. Ask yourself why the loop is never run.
     
    Fred Kleinschmidt
    Bartender
    Posts: 571
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Your try block is too tightly placed.
    scan.nextInt() can throw exceptions, so they should be inside the try clause rather than before it.
     
    Campbell Ritchie
    Marshal
    Posts: 56546
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stephan van Hulst wrote:. . .
  • Dividing by zero is a bug, which should not be recoverable. DenominatorIsZero should be a RuntimeException.
  • . . .
    Agre; it should extend ArithmeticException and its name should end with Exception.
    If a zero is passed to the constructor, you should throw an IllegalArgumentException. You should also describe that non‑zero requirement in a documentation comment.
     
    Leoncio Thiago
    Greenhorn
    Posts: 11
    Eclipse IDE Java Objective C
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Dawn Goxhaj wrote:so I am doing a fraction exception test with a denominator is zero exception. everything compiles except the actual FractionExceptionTest which gives me one error in the catch which is: error: exception DenominatorIsZero is never thrown in body of corresponding try statement
                catch(DenominatorIsZero e)
                ^
    1 error

    I have found a plethora of reasons for this but am falling victim to information overload and am getting nowhere. Below are all 3 parts of the code.  hoping someone can help. 

    Thanks in advance!!
    MJ





    Hi Dawn,

    I would say try to always simplify your code. So, instead of using all these classes you can just take care of the denominator zero with the exception. Like this
                   try {
    int num1 = 30, num2 = 0;
    int output = num1 / num2;
    System.out.println("Result = " + output);
    } catch (ArithmeticException e) {
    System.out.println("[PhoneCoding.com-INFO LOG] Arithmetic Exception: You can't divide an integer by 0");
    }


    Simple is always better.

    --Thiago
     
    Knute Snortum
    Sheriff
    Posts: 4279
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Simple is always better. 

    Almost always.

  • Safety over Clarity
  • Clarity over Brevity
  • Brevity over all else
  •  
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!