views:

610

answers:

5

I have an I2C device that wants two inputs: a denominator and a numerator. Both are written to separate addresses, so no actual calculation (numerator/denominator) is done. The problem with this is that a divide by zero could occur on the I2C device, so a divide by zero error needs to be checked for. Ideally, exactly the same thing would happen if the dividing were done by the java code.

At the moment, I've bodged an unused variable that does the division, but I'm worried it'll get optimized out:

public void setKp(int numerator, int divisor)
{
    int zeroCheck = numerator / divisor;
    //... doesn't use zeroCheck
}

Surely there's a better way!

+9  A: 

Do this:

if (denominator == 0) throw new ArithmeticException("denominator == 0!");

ArithmeticException is the exception which is normally thrown when you divide by 0.

uckelman
+2  A: 

Something like:

if(divisor == 0) {
  throw new ArithmeticException("Division by zero!");
}
Bart Kiers
+4  A: 
public class ZeroDivisionException extends ArithmeticException {
    // ...
}

if (denominator == 0) {
    throw new ZeroDivisionException();
}
JG
+6  A: 

You should not throw an ArithmeticException. Since the error is in the supplied arguments, throw an IllegalArgumentException. As the documentation says:

Thrown to indicate that a method has been passed an illegal or inappropriate argument.

Which is exactly what is going on here.

if (divisor == 0) {
    throw new IllegalArgumentException("Argument 'divisor' is 0");
}
Joren
The OP specified: "Ideally, exactly the same thing would happen if the dividing were done by the java code."
Yishai
I concur. My answer was only addressing how to get the same behavior as you would if you actually divided by zero, not whether you should.
uckelman
Yishai, yes, but felt it was appropriate to second-guess that assertion.
Joren
There is some debate about this view. For example, some feel (as you would, I assume) that a null argument given to a method that doesn't accept null should cause an IllegalArgumentException to be thrown, while others believe that a NullPointerException should be thrown in this situation.
Laurence Gonsalves
I think NullPointerException should never be thrown manually, instead throwing IllegalArgumentException whenever a null is incorrectly passed to a method. If that's done, and you get a NullPointerException from a method, you are sure there is a bug in that code. If the method throws an IllegalArgumentException, you know that there is a bug in *your* code. I think that's an incredibly useful distinction. That said, I usually program in C#, and the distinction between NullReferenceException and ArgumentNullException is pretty clear on .NET. In Java I'm not so sure the intended design is the same
Joren
+1 The IllegalArgumentException is much more clear and specific. Especially if setKp(int numerator, int divisor) is a part of a public API. I second Joren on the NullPointerException not to be thrown manually (even though standard usage is disagrees). See this question http://stackoverflow.com/questions/3881/illegalargumentexception-or-nullpointerexception-for-a-null-parameter
Fedearne
+2  A: 

There are two ways you could do this. Either create your own custom exception class to represent a divide by zero error or throw the same type of exception the java runtime would throw in this situation.

Define custom exception

public class DivideByZeroException() extends ArithmeticException {
}

Then in your code you would check for a divide by zero and throw this exception:

if (divisor == 0) throw new DivideByZeroException();

Throw ArithmeticException

Add to your code the check for a divide by zero and throw an arithmetic exception:

if (divisor == 0) throw new java.lang.ArithmeticException("/ by zero");

Additionally, you could consider throwing an illegal argument exception since a divisor of zero is an incorrect argument to pass to your setKp() method:

if (divisor == 0) throw new java.lang.IllegalArgumentException("divisor == 0");
slangevi
Sorry for the duplicate answer. Seems you have to be quick to answer questions! :)
slangevi