gpt4 book ai didi

java - 在 Java 中抛出 ArithmeticException() 的所有无效操作的列表?

转载 作者:行者123 更新时间:2023-11-29 04:34:47 25 4
gpt4 key购买 nike

是否有在 Java 中抛出 ArithmeticException 的所有可能操作(例如除以零)的完整列表?

换句话说:例如,可以将这些异常捕获为

catch (ArithmeticException e) {
e.printStackTrace();
}

对于除以零,消息将显示为:

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

当然,此消息是编译器可用的预定义数量。所以我的问题是,在哪里可以找到编译器可能生成的算术异常消息的完整列表?

最佳答案

没有完整的列表,但以下是一些原因。

好像有两种:

  1. 除以零。
  2. 请求准确结果时的结果不准确(例如溢出或舍入)。

到目前为止,这是我发现的:

  • Normal and Abrupt Completion of Evaluation (JLS §15.6) :

    An integer division (§15.17.2) or integer remainder (§15.17.3) operator throws an ArithmeticException if the value of the right-hand operand expression is zero.

  • Math.addExact(int x, int y) ,
    Math.subtractExact(int x, int y),
    Math.multiplyExact(int x, int y),
    Math.incrementExact(int a),
    Math.decrementExact(int a),
    Math.negateExact(int a):

    Throws: ArithmeticException - if the result overflows an int

  • Math.addExact(long x, long y),
    Math.subtractExact(long x, long y),
    Math.multiplyExact(long x, long y),
    Math.incrementExact(long a),
    Math.decrementExact(long a),
    Math.negateExact(long a):

    Throws: ArithmeticException - if the result overflows an long

  • Math.toIntExact(long 值):

    Throws: ArithmeticException - if the argument overflows an int

  • Math.floorDiv(int x, int y),
    Math.floorDiv(long x, long y),
    Math.floorMod(int x, int y),
    Math.floorMod(long x, long y):

    Throws: ArithmeticException - if the divisor y is zero

  • BigDecimal.ROUND_UNNECESSARY :

    If this rounding mode is specified on an operation that yields an inexact result, an ArithmeticException is thrown.

  • BigDecimal.divide(BigDecimal divisor, int scale, int roundingMode),
    BigDecimal.divide(BigDecimal divisor, int scale, RoundingMode roundingMode),
    BigDecimal.divide(BigDecimal divisor, int roundingMode),
    BigDecimal.divide(BigDecimal 除数, RoundingMode roundingMode),
    ...:

    Throws: ArithmeticException - if divisor is zero

关于java - 在 Java 中抛出 ArithmeticException() 的所有无效操作的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42120024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com