[英]Java division by zero doesnt throw an ArithmeticException - why?
[英]List of all invalid operations that throw an ArithmeticException() in Java?
是否存在在Java中拋出ArithmeticException
的所有可能操作(例如除零)的完整列表?
換句話說:例如,人們可以將這些例外記錄為
catch (ArithmeticException e) {
e.printStackTrace();
}
對於除零,消息將顯示為:
java.lang.ArithmeticException: / by zero
at [filename:line number]
當然,此消息是編譯器可用的預定義數量。 所以我的問題是,在哪里可以找到編譯器可能生成的完整的arithmeticexception消息列表?
沒有完整的清單,但下面是一些原因。
似乎有兩種類型:
到目前為止,這是我發現的:
如果右側操作數表達式的值為零,則整數除法(第15.17.2節 )或整數余數(第15.17.3節 )運算符拋出
ArithmeticException
。
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)
:
拋出:
ArithmeticException
- 如果結果溢出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)
:
拋出:
ArithmeticException
- 如果結果溢出long
Math.toIntExact(long value)
:
拋出:
ArithmeticException
- 如果參數溢出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)
:
拋出:
ArithmeticException
- 如果除數y為零
BigDecimal.ROUND_UNNECESSARY
:
如果在產生不精確結果的操作上指定了此舍入模式,則拋出
ArithmeticException
。
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)
,
...:
拋出:
ArithmeticException
- 如果除數為零
這里是通過在JRE8中搜索ArithmeticException的構造函數而使用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)
這不包含調用此方法的方法...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.