Can anyone help me find where the execption is? I can't seem to find the problem..
public void fieldChanged(Field f, int context){
//if the submit button is clicked
try{
stopTime = System.currentTimeMillis();
timeTaken = stopTime - startTime;
timeInSecs = ((timeTaken/1000));
speed = 45/timeInSecs;
Dialog.alert("Speed of Delivery: " + speed + "mph");
}
catch(ArithmeticException e){
Dialog.alert("error " + speed);
e.printStackTrace();
}
}
startTime variable is a global variable..
edit:
how can timeinSecs = 0? I cant seem to get my debugger working for the BlackBerry JDE so someone will have to help me out :( timeTaken should be the time in ms from pushing the point of pushing a start button to the pont of pushing the stop button...
all other variables are global as well
解决方案
Exceptions have types, and what this allows is for you to look up the type and quickly categorize the problem. From the documentation:
ArithmeticException:
Thrown when an exceptional arithmetic condition has occurred. For example, an integer "divide by zero" throws an instance of this class.
Moreover, most exceptions are constructed with a message to help you even further figure out what happened.
try {
int i = 0 / 0;
} catch (ArithmeticException e) {
e.printStackTrace();
}
This prints:
java.lang.ArithmeticException: / by zero
at [filename:line number]
But how did this happen?
Java, like many other programming languages, distinguishes between integer division and floating point division.
The binary / operator performs division, producing the quotient of its operands. The left-hand operand is the dividend and the right-hand operand is the divisor. Integer division rounds toward 0. [...] if the value of the divisor in an integer division is 0, then an ArithmeticException is thrown.
The following may surprise you if you're not familiar with integer division:
System.out.println(1/2); // prints "0"
What happens here is that since both the dividend and the divisor are int, the operation is an integer division, whose result is rounded to an int. Remember that an int can only contain whole number (of limited range, some 4 billion numbers approximately).
You can specify that you need a floating point division by making at least one of the operands a floating point number.
System.out.println(1/2.0); // prints "0.5"
System.out.println(1D/2); // prints "0.5"
D is a special suffix for numeric literal to specify that it's a double-precision value. There's also L for long (64-bit integer).
A double value needs to be stored in a double variable.
double v = 1D / 2; // v == 0.5
int i = 1D / 2; // DOESN'T COMPILE!!! Explicit cast needed!
Note that which division is performed doesn't have anything to do with what type it'll eventually go to. It only depends on what type the dividend and divisor are.
double v = 1 / 2; // v == 0.0 (!!!)
You should also note that double too, is a limited precision number.
System.out.println(.2D + .7D - .9D); // prints "-1.1102230246251565E-16"
But what about my code?
So now, let's focus on what happened with your code:
timeTaken = stopTime - startTime;
timeInSecs = ((timeTaken/1000));
speed = 45/timeInSecs;
More than likely what happened is that timeTaken is declared as a long. Therefore timeTaken/1000 results in integer division. If timeTaken < 1000, the result of the division is 0.
At this point, it doesn't matter if timeInSecs is a double or a float, because the integer division has already been performed. This means that timeInSecs would be either 0 or 0.0, depending on its type.
From the error you get, though, one can determine that timeInSecs is likely to be an integer type. Otherwise, 45/timeInSecs would result in a floating point division that results in Infinity (a special double value) instead of throwing ArithmeticException.
So how do we fix this?
We can fix this by declaring the variables as follows:
long timeTaken;
double timeInSecs;
double speed;
And then performing the calculation as follows (note that 1000 is now a double value).
timeTaken = stopTime - startTime;
timeInSecs = timeTaken/1000D;
speed = 45D/timeInSecs; // D is not necessary here, but it's good for clarity
See also
这篇博客详细解析了在Java中遇到ArithmeticException异常,特别是`除以零`的情况。作者通过示例代码说明了整数除法可能导致的问题,并提供了如何正确进行浮点数除法以避免异常的解决方案。文章强调了变量类型的重要性,特别是在涉及数值运算时,建议将变量声明为适当的类型,如在计算速度时使用double类型。最后,给出了修正代码的建议,以确保时间计算的准确性。

1万+

被折叠的 条评论
为什么被折叠?



