当前页面: 开发资料首页 → J2EE 专题 → 为什么1/0不抛出异常
为什么1/0不抛出异常
摘要: 为什么1/0不抛出异常
package base;
public class Test {
public static double test(double a,double b){
return a/b;
}
public static void main(String[] args){
try{
double i =test(1,0);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
public static double test(int a,int b)
{
return a/b;
}
double 的话 0.0d / 0.0 是 NaN,所以没异常
那为什么在函数里面直接double = 1/0就异常呢?
1/0会认为是int的运算,所以会有异常,改成1.0/0就可以了