Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, August 6, 2016

How should I throw a divide by zero exception in Java without actually dividing by zero?

How should I throw a divide by zero exception in Java without actually dividing by zero?


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!

Answer by uckelman for How should I throw a divide by zero exception in Java without actually dividing by zero?


Do this:

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

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

Answer by Bart Kiers for How should I throw a divide by zero exception in Java without actually dividing by zero?


Something like:

if(divisor == 0) {    throw new ArithmeticException("Division by zero!");  }  

Answer by Jo?o Silva for How should I throw a divide by zero exception in Java without actually dividing by zero?


public class ZeroDivisionException extends ArithmeticException {      // ...  }    if (denominator == 0) {      throw new ZeroDivisionException();  }  

Answer by Joren for How should I throw a divide by zero exception in Java without actually dividing by zero?


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");  }  

Answer by slangevi for How should I throw a divide by zero exception in Java without actually dividing by zero?


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");  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.