c# - 在 Visual Studio 中捕获整数溢出异常

标签 c# .net visual-studio exception integer

如何让 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.

在项目的项目属性中,转到“构建”选项卡并单击“高级...”按钮。此窗口上有一个“检查算术上溢/下溢”复选框:

enter image description here

顺便说一句,(byte)(bigNumber) 中的第二组括号不是必需的:(byte)bigNumber

关于c# - 在 Visual Studio 中捕获整数溢出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11467736/

相关文章:

c# - 优化 Lucene 批量索引

c# - 如何写入自定义事件日志?

c# - 字段或属性\"ListItemAllFields\"不存在异常

.net - 强制关闭应用程序 (.net)

c++ - 最大缩减 Openmp 2.0 Visual Studio 2013/C++

c++ - 如果删除未使用的引用类,则 DLL 无法加载

c# - 是否可以强制泛型类从两个接口(interface)之一继承类型?

.net - 无法解决 Azure 应用服务中的 (502) 错误网关错误

.net - 在字节模式和异步中重用 NamedPipeServerStream 的正确方法

visual-studio - 在 VS 2012 Premium 中删除未使用的 Using