Newb! Help with try/catch

Dawn Goxhaj

Greenhorn

Posts: 14

posted 1 year ago

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

posted 1 year ago

Welcome to CodeRanch, Dawn!

We can't really help you unless you post the full error message, and your Fraction class.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.

Dawn Goxhaj

Greenhorn

Posts: 14

posted 1 year ago

oops. sorry about that!
here's my fraction class:

Dawn Goxhaj

Greenhorn

Posts: 14

posted 1 year ago

geez. sorry again for messing up. guess I've become too frustrated with this :/

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.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.

Dawn Goxhaj

Greenhorn

Posts: 14

posted 1 year ago

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

posted 1 year ago

That issue is raised in my third bullet point. Ask yourself why the loop is never run.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.

Fred Kleinschmidt

Bartender

Posts: 571

9

posted 1 year ago

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

posted 1 year ago

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.