ArithmeticException Java? [英] ArithmeticException Java?

查看:117
本文介绍了ArithmeticException Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮我找到执行的地方吗?我似乎无法找到问题..

  public void fieldChanged(Field f,int context){
//如果单击提交按钮
try {
stopTime = System.currentTimeMillis();
timeTaken = stopTime - startTime;
timeInSecs =((timeTaken / 1000));
speed = 45 / timeInSecs;
Dialog.alert(交货速度:+速度+mph);
}
catch(ArithmeticException e){
Dialog.alert(error+ speed);
e.printStackTrace();

}

}

startTime变量是全局变量..



编辑:
timeinSecs = 0怎么办?我似乎无法让我的调试器为BlackBerry JDE工作,所以有人必须帮助我:( timeTaken应该是从按下启动按钮到按下停止按钮的按钮的时间毫秒...... / p>

所有其他变量都是全局的

解决方案

例外有类型,这允许您查找类型并快速对问题进行分类。来自文档:


ArithmeticException
在发生异常算术条件时抛出。例如,一个整数除以零抛出此类的实例。


此外,大多数例外情况都会构建一条消息,以帮助您进一步弄清楚发生了什么。

 试试{
int i = 0/0;
} catch(ArithmeticException e){
e.printStackTrace();
}

打印:

  java.lang.ArithmeticException:/ by $ 
at [filename:line number]

但是这是怎么发生的?



Java,和许多其他一样编程语言,区分整数除法和浮点除法。



JLS 15.17.2分部操作员/




二进制 / 运算符执行除法,产生其操作数的商。左手操作数是被除数,右手操作数是除数。整数除法向0舍入为0. [...]如果整数除数中的除数值为0,则抛出 ArithmeticException


如果您不熟悉整数除法,以下内容可能会让您感到惊讶:

  System.out.println(1/2); //打印0

这里发生的是因为红利和除数都是 int ,该操作是整数除法,其结果舍入为 int 。请记住, int 只能包含整数(范围有限,约为40亿个数字)。



你可以通过使至少一个操作数成为浮点数来指定您需要浮点除法。

  System.out。的println(1 / 2.0); //打印0.5
System.out.println(1D / 2); //打印0.5

D 是数字文字的特殊后缀,用于指定它是 double -precision值。对于 long (64位整数),还有 L



double 值需要存储在 double 变量中。

  double v = 1D / 2; // v == 0.5 
int i = 1D / 2; //没有编译!!!需要明确的演员!

请注意,执行哪个分区与它最终会进入的类型没有任何关系至。它只取决于股息和除数的类型。

  double v = 1/2; // v == 0.0(!!!)

你还应注意 double 也是一个有限的精确数。

  System.out.println(.2D + .7D  -  .9D); //打印-1.1102230246251565E-16

但我的代码怎么样?



现在,让我们关注你的代码发生了什么:

  timeTaken = stopTime  -  startTime; 
timeInSecs =((timeTaken / 1000));
speed = 45 / timeInSecs;

很可能发生的事情是 timeTaken 声明为 long 。因此 timeTaken / 1000 会产生整数除法。如果 timeTaken< 1000 ,除法的结果是 0



此时,它如果 timeInSecs double float ,因为已经执行了整数除法。这意味着 timeInSecs 将是 0 0.0 ,取决于它的类型。



从你得到的错误中,可以确定 timeInSecs 可能是整数类型。否则, 45 / timeInSecs 将导致浮点除法,导致无穷大(一个特殊的 double value)而不是抛出 ArithmeticException



那么我们如何解决这个问题呢?



我们可以通过声明变量来解决这个问题:

  long timeTaken; 
double timeInSecs;
倍速;

然后按如下方式执行计算(请注意 1000 现在是 double 值。)

  timeTaken = stopTime - 开始时间; 
timeInSecs = timeTaken / 1000D;
speed = 45D / timeInSecs; //这里不需要D,但它有利于清晰度



参见




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.

JLS 15.17.2 Division Operator /

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

这篇关于ArithmeticException Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆