java.lang.ArithmeticException:除法未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34329198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.ArithmeticException: Division is undefined
提问by CHS
I have a simple operation going on in my program:
我的程序中有一个简单的操作:
exposureNoDecimals =
BigDecimal.valueOf(curEffSpreadPremium).multiply(BigDecimal.valueOf(100)).divide(wsRate, 0,
java.math.RoundingMode.HALF_UP).longValue();
exposureNoDecimals - long curEffSpreadPremium - long wsRate - BigDecimal
ExposureNoDecimals - long curEffSpreadPremium - long wsRate - BigDecimal
However I am getting
但是我得到
"java.lang.ArithmeticException: Division is undefined"
at java.math.BigDecimal.longScaledDivide(BigDecimal.java:3105)
at java.math.BigDecimal.divide(BigDecimal.java:2409)
at java.math.BigDecimal.divide(BigDecimal.java:2396)
at java.math.BigDecimal.divide(BigDecimal.java:2361)
The problem is the issue is recreatable on production and not on my machine (cant debug, or cant see the inputs)
问题是该问题可在生产中重新创建,而不是在我的机器上(无法调试,或无法看到输入)
What can be the issue here? Any suggestions/ideas?
这里有什么问题?任何建议/想法?
回答by Stephen C
Take a look at the source code for BigDecimal
(e.g. here).
看一看BigDecimal
(例如这里)的源代码。
An ArithmeticException
is only thrown with the message "Division undefined" when you attempt to divide zero by zero.
一个ArithmeticException
只与“司未定义”当您尝试除以零零消息抛出。
I'm not going to suggest a fix, because the >>correct<< fix will depend on what this calculation is supposed to be doing, and why the divisor / dividend happen to be zero. Putting in some zero checks might be a solution, but it could also be a "band-aid solution" that hides the problem rather than fixing it. It could come back to bite you later on.
我不打算建议修复,因为>>正确<< 修复将取决于此计算应该做什么,以及为什么除数/被除数恰好为零。进行一些零检查可能是一个解决方案,但它也可能是隐藏问题而不是修复问题的“创可贴解决方案”。以后它可能会回来咬你。
回答by Hendrik
According to the source code of BigDecimal
, java.lang.ArithmeticException: Division undefined
(without the is) is only thrown when you divide zero by zero.
根据BigDecimal
, java.lang.ArithmeticException: Division undefined
(没有is)的源代码仅在您将零除以零时才会抛出。
Looks like in your case curEffSpreadPremium
andwsRate
both are zero.
看起来在你的情况下curEffSpreadPremium
,wsRate
两者都是零。
So you need to guard the line with zero-checks.
所以你需要用零检查来保护线路。