• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Newb! Help with try/catch

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
Saloon Keeper
Posts: 15727
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Dee Gx
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops.  sorry about that!  
here's my fraction class:

 
Dee Gx
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
geez.  sorry again for messing up.  guess I've become too frustrated with this :/




 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15727
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
  •  
    Dee Gx
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 15727
    368
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    That issue is raised in my third bullet point. Ask yourself why the loop is never run.
     
    Bartender
    Posts: 732
    10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Marshal
    Posts: 79961
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Greenhorn
    Posts: 11
    Eclipse IDE Objective C Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Sheriff
    Posts: 7125
    184
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Simple is always better.  


    Almost always.

  • Safety over Clarity
  • Clarity over Brevity
  • Brevity over all else
  •  
    For my next feat, I will require a volunteer from the audience! Perhaps this tiny ad?
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic