自动引发NaN或Infinity值的异常 [英] Automatic raise of exception for NaN or Infinity values

查看:263
本文介绍了自动引发NaN或Infinity值的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一长串计算(使用double类型的变量),其中某些操作可能会导致(取决于输入值)NaN或Infinity。

因为我不喜欢我想添加如下内容:

I have a long sequence of calculations (using variables of type "double") where some operations might result (depending on input values) in "NaN" or "Infinity".
Since I don't want to add something like the following:

if ((double.IsInfinity(result)) || (double.NaN(result))) {
...
}



每次操作后,我想为这样的结果激活异常的自动加注,然后只是在第一个错误结果后捕获异常停止。



我试过这个:

您也可以尝试启用检查算术溢出/下溢。它位于项目属性中的Build-> Advanced Build Settings下。

当它打开时,将抛出算术异常以进行溢出和下溢(而不是换行)。它可能适用于ln函数,也可能不适用于ln函数。试一试。


和这个:

要捕获抛出特定异常的位置,当异常发生时你可以(暂时)中断抛出调试器。

选择Debug>例外然后展开树以选择Common Language Runtime Exceptions>系统> System.ArithmeticException并检查Thrown选项。


但是在除零后仍然没有引发异常。

有什么建议吗?

谢谢。


after each operation, I would like to activate automatic raise of exception for such results, and then just catch the exception to stop after the first "wrong" result.

I tried this:
You can also try turning on "Check for arithmetic overflow/underflow." It is located in your project properties, under "Build->Advanced Build Settings"
When it is turned on, arithmetic exceptions will be thrown for an overflow and underflow (instead of wrapping). It may or may not apply to the ln function. Give it a try.

and this:
To trap where a specific exception is being thrown you could (temporarily) break when the exception is thrown in the debugger.
Select Debug > Exceptions then expand the tree to select Common Language Runtime Exceptions > System > System.ArithmeticException and check the "Thrown" option.

but still got no exceptions raised after a division by zero.
Any suggestions?
Thank you.

推荐答案

你好,



我想你需要加薪像你这样的例外:

Hello,

I think you'll need to raise the exception yourself like this :
if ((double.IsInfinity(result)) || (double.NaN(result))) {
   throw new ArithmeticException();
}


这篇关于自动引发NaN或Infinity值的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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