如此简单的一个异常捕获怎么捕获不了

帅气好男人_Jack 2014-06-18 01:50:47
刚刚学习java,写了一个加减乘除的简单程序,有两个捕获,出问题的捕获是对除法--除数为0的捕获,为何却一直捕获不了;程序很简单,就是不知道为什么?
//利用命令行参数 做计算器
public class TestCal
{
public static void main(String[] args)
{
if(args.length < 3)
{
System.out.println("input parameter number is under 3");
System.exit(-1);
}
double d1=0,d2=0;
try
{
d1 = Double.parseDouble(args[0]); //parseDouble是一个static函数,故直接用类名就可以调用
d2 = Double.parseDouble(args[2]);
}catch(NumberFormatException NE)
{
System.out.println("input parameter include string!");
NE.printStackTrace();
System.exit(-1);
}
double Result = 0;

try
{
if(args[1].equals("+"))
Result = d1 + d2;
else if(args[1].equals("-"))
Result = d1 - d2;
else if(args[1].equals("×"))
Result = d1 * d2;
else if(args[1].equals("/")) //此种情况下,d2为0 下面的catch却不执行 捕获不到 ?????
Result = d1 / d2;
else
{
System.out.println("the secaond paramter shoulde be operator");
System.exit(-1);
}
}catch(ArithmeticException AE) //除数为0捕获
{
System.out.println("divisor is 0!");
AE.printStackTrace();
System.exit(-1);
}
System.out.println(args[0]+args[1]+args[2]+ " = " + Result);
}
}

下面是程序执行时截图:
...全文
449 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
姜小白- 2014-06-19
  • 打赏
  • 举报
回复
引用 11 楼 qlbssq 的回复:
try { double i = 10; System.out.println(1/0); } catch (ArithmeticException e){ System.out.println("除数不能为0"); } 不是说double c除以0 不会报异常吗 为何我上面的代码确能捕获异常
你定义了double 的i ,但用的是int的 1 / 0 做运算的。
lliiqiang 2014-06-19
  • 打赏
  • 举报
回复
结果也告诉你可以这么做,结果是无穷.这迎合了数学的极限概念。
lliiqiang 2014-06-19
  • 打赏
  • 举报
回复
double中0可以作为除数.
shine333 2014-06-19
  • 打赏
  • 举报
回复
从LZ的代码看,LZ只是要判断divisor is 0! 这个只要写if即可,if 除数为 0
引用 16 楼 qlbssq 的回复:
引用
12
java 不是有自动类型转换吗 doubel / int 结果不是应该为double类型吗
你哪行代码是double / int了????
引用 11 楼 qlbssq 的回复:
try { double i = 10; System.out.println(1/0); } catch (ArithmeticException e){ System.out.println("除数不能为0"); } 不是说double c除以0 不会报异常吗 为何我上面的代码确能捕获异常
欧煞旁人 2014-06-19
  • 打赏
  • 举报
回复
引用
12
java 不是有自动类型转换吗 doubel / int 结果不是应该为double类型吗
姜小白- 2014-06-19
  • 打赏
  • 举报
回复
引用 13 楼 jackzhouyu 的回复:
那这个怎么办呢?算是语言的漏洞或者不规范
看7楼的链接内容,double的运算,java就是这么个规则,除数就是可以为0,然后结果为正负无穷大。 楼主要捕获异常,可以将数据类型转换一下。
  • 打赏
  • 举报
回复
引用 1 楼 stonefeng 的回复:
把ArithmeticException换成RuntimeException试试
不行还是一样的,也许这是java特有的机制,double类型不适应除数为0的情况
  • 打赏
  • 举报
回复
引用 2 楼 stonefeng 的回复:
或者干脆用Exception
那这个怎么办呢?算是语言的漏洞或者不规范
shine333 2014-06-18
  • 打赏
  • 举报
回复
引用 11 楼 qlbssq 的回复:
try { double i = 10; System.out.println(1/0); } catch (ArithmeticException e){ System.out.println("除数不能为0"); } 不是说double c除以0 不会报异常吗 为何我上面的代码确能捕获异常
你是double的除法吗???? 1.0/0.0 vs 1/0.0 vs 1.0/0 vs 1/0
欧煞旁人 2014-06-18
  • 打赏
  • 举报
回复
try { double i = 10; System.out.println(1/0); } catch (ArithmeticException e){ System.out.println("除数不能为0"); } 不是说double c除以0 不会报异常吗 为何我上面的代码确能捕获异常
欧煞旁人 2014-06-18
  • 打赏
  • 举报
回复
感谢楼主 让我们了解了这个知识点
miracleliu 2014-06-18
  • 打赏
  • 举报
回复
疯癫行者 2014-06-18
  • 打赏
  • 举报
回复
学习了,
姜小白- 2014-06-18
  • 打赏
  • 举报
回复
15.17.2. Division Operator / The result of a floating-point division is determined by the rules of IEEE 754 arithmetic: If either operand is NaN, the result is NaN. If the result is not NaN, the sign of the result is positive if both operands have the same sign, and negative if the operands have different signs. Division of an infinity by an infinity results in NaN. Division of an infinity by a finite value results in a signed infinity. The sign is determined by the rule stated above. Division of a finite value by an infinity results in a signed zero. The sign is determined by the rule stated above. Division of a zero by a zero results in NaN; division of zero by any other finite value results in a signed zero. The sign is determined by the rule stated above. Division of a nonzero finite value by a zero results in a signed infinity. The sign is determined by the rule stated above.
xlight2023 2014-06-18
  • 打赏
  • 举报
回复
		System.out.println(1.0 / 0);//打印"Infinity"
		System.out.println(-1.0 / 0);//打印"-Infinity"
		System.out.println(0.0 / 0.0);//打印"NaN"
Infinity是无穷大的意思。 这个是java算术的一个机制。
xander_wen 2014-06-18
  • 打赏
  • 举报
回复
3楼正解 !
S117 2014-06-18
  • 打赏
  • 举报
回复
别用Double,换其他的
剑神一笑 2014-06-18
  • 打赏
  • 举报
回复
double 0 .0做除数的时候是不会抛异常的 原因是java中有三个特殊的浮点类型,就是NAN,负无穷大和无穷大 INFINITY就是无限大的意思
疯癫行者 2014-06-18
  • 打赏
  • 举报
回复
或者干脆用Exception
疯癫行者 2014-06-18
  • 打赏
  • 举报
回复
把ArithmeticException换成RuntimeException试试

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧