(这是一个后续问题为什么这个异常没有打印出来?为什么显示错误? https://stackoverflow.com/questions/12130659/why-is-this-exception-is-not-printed-why-is-it-showing-an-error)
在下面的代码中,为什么没有触发 ArithmeticException?
class Exp
{
public static void main(String args[])
{
float d,a=1;
try
{
d=0;
a=44/d; //no exception triggered here.. why?
System.out.print("It's not gonna print: a="+a);
}
catch(ArithmeticException e)
{
System.out.println("Print exception: "+e);
}
}
}
相反,输出是:
It's not gonna print: a=Infinity
会发生什么?
除以零会引发整数值异常,但不会引发浮点值异常。这是定义在JLS #15.17.2 http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.17.2:
浮点除法的结果由 IEEE 754 算术规则确定:
[...]
- 非零有限值除以零会得到带符号的无穷大。符号由上述规则确定。
如果你改变类型a
and d
to int
,你会得到一个例外。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)