如何让 Visual Studio 抛出整数溢出异常?
我知道我可以明确地使用 checked
语句。但我想在我的解决方案中将此行为设为默认(至少在调试期间)。
int bigNumber= 12345;
byte overflowException = (byte)(bigNumber); //want to see an exception here
最佳答案
根据 the docs有一个 /checked
编译器选项可以默认打开它。
The /checked compiler option lets you specify checked or unchecked context for all integer arithmetic statements that are not explicitly in the scope of a checked or unchecked keyword.
在项目的项目属性中,转到“构建”选项卡并单击“高级...”按钮。此窗口上有一个“检查算术上溢/下溢”复选框:
顺便说一句,(byte)(bigNumber)
中的第二组括号不是必需的:(byte)bigNumber
。
关于c# - 在 Visual Studio 中捕获整数溢出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11467736/