有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java中引发算术异常()的所有无效操作的异常列表?

是否有一个在Java中抛出ArithmeticException的所有可能操作(如除零)的综合列表

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

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

对于被零除的情况,信息如下:

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

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


共 (2) 个答案

  1. # 1 楼答案

    没有完整的清单,但以下是一些原因

    似乎有两种类型:

    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.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 value)

      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

    • ^{}

      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 divisor, RoundingMode roundingMode)
      ...:

      Throws: ArithmeticException - if divisor is zero

  2. # 2 楼答案

    下面是通过在JRE8中搜索算术异常的构造函数,使用Eclipse创建的列表:

    java.lang - rt.jar - C:\Program Files\Java\JRE8\lib - Test
        Math
            addExact(int, int)
            addExact(long, long)
            decrementExact(int)
            decrementExact(long)
            incrementExact(int)
            incrementExact(long)
            multiplyExact(int, int)
            multiplyExact(long, long)
            negateExact(int)
            negateExact(long)
            subtractExact(int, int)
            subtractExact(long, long)
            toIntExact(long)
    java.math - rt.jar - C:\Program Files\Java\JRE8\lib - Test
        BigDecimal
            byteValueExact()
            checkScale(BigInteger, long)
            checkScale(long, long)
            checkScale(long)
            checkScaleNonZero(long)
            commonNeedIncrement(int, int, int, boolean)
            divide(BigDecimal, MathContext) (2 matches)
            divide(BigDecimal) (3 matches)
            divideToIntegralValue(BigDecimal, MathContext)
            intValueExact()
            longValueExact() (2 matches)
            pow(int, MathContext) (2 matches)
            pow(int)
            shortValueExact()
        BigInteger
            BigInteger(int, int, Random)
            byteValueExact()
            clearBit(int)
            flipBit(int)
            getPrimeSearchLen(int)
            intValueExact()
            longValueExact()
            mod(BigInteger)
            modInverse(BigInteger)
            modPow(BigInteger, BigInteger)
            nextProbablePrime()
            pow(int)
            probablePrime(int, Random)
            reportOverflow()
            setBit(int)
            shortValueExact()
            testBit(int)
        LongOverflow
            check(BigDecimal)
        MutableBigInteger
            divide(long, MutableBigInteger)
            divideKnuth(MutableBigInteger, MutableBigInteger, boolean)
            euclidModInverse(int) (2 matches)
            modInverse(MutableBigInteger)
            modInverseMP2(int)
            mutableModInverse(MutableBigInteger)
    java.text - rt.jar - C:\Program Files\Java\JRE8\lib - Test
        DigitList
            shouldRoundUp(int, boolean, boolean)
    java.time - rt.jar - C:\Program Files\Java\JRE8\lib - Test
        Duration
            create(BigDecimal)
            dividedBy(long)
    

    此不包含调用此方法的方法