为什么这段代码不会抛出ArithmeticException?看一看:
public class NewClass {
public static void main(String[] args) {
// TODO code application logic here
double tab[] = {1.2, 3.4, 0.0, 5.6};
try {
for (int i = 0; i < tab.length; i++) {
tab[i] = 1.0 / tab[i];
}
} catch (ArithmeticException ae) {
System.out.println("ArithmeticException occured!");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道!
我有一个需要两个输入的I2C设备:分母和分子.两者都写入单独的地址,因此不进行实际的计算(numerator/denominator).这样做的问题是在I2C器件上可能会出现除零,因此需要检查除零误差.理想情况下,如果划分由java代码完成,则会发生完全相同的事情.
目前,我已经提供了一个未使用的变量进行划分,但我担心它会被优化掉:
public void setKp(int numerator, int divisor)
{
int zeroCheck = numerator / divisor;
//... doesn't use zeroCheck
}
Run Code Online (Sandbox Code Playgroud)
当然有更好的方法!