为什么不在这里触发例外? [英] Why the exception is not triggered here?

查看:132
本文介绍了为什么不在这里触发例外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于为什么不打印这个异常?为什么显示错误?


下面的代码为什么没有触发ArithmeticException?

This is a follow up question of Why is this exception is not printed? Why it's showing an error?

Here in below code why the ArithmeticException is not triggered?

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("Its not gonna printed a="+a); 
        }
        catch(ArithmeticException e)
        {
            System.out.println("Print exception"+e);
        }
    }
} 

而是输出如下:

Its not gonna printed a=Infinity

发生什么?

推荐答案

一个除以零的整数值抛出异常,但不适用于浮动值。这在 JLS#15.17.2

A division by zero throws an exception for integer values, but not for floating values. This is defined in the JLS #15.17.2:


浮点除法的结果由IEEE 754算术规则确定:

[...]

The result of a floating-point division is determined by the rules of IEEE 754 arithmetic:
[...]


  • 由零产生的非零有限值的划分导致带符号的无穷大。符号由上述规则确定。

如果您更改 a的类型 d int ,您将收到例外。

If you change the type of a and d to int, you will get an exception.

这篇关于为什么不在这里触发例外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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