如何在C++中捕获数据类型溢出异常。

发布于 2025-02-06 08:06:41 字数 187 浏览 0 评论 0原文

在这里,基本可能会超出INT的限制,从而导致运行时错误,这时我打算捕获运行时错误并处理它,因此我尝试了Try-Catch块,但没有被捕获。

int base=1;
try
{
     base *= 10;
     //some code
}
catch(...)
{
     //some code
}

Here, base may overflow beyond limits of int, causing runtime error, at which point I intend to catch the Runtime error raised, and handle it, so I tried the try-catch block, but it is not being caught.

int base=1;
try
{
     base *= 10;
     //some code
}
catch(...)
{
     //some code
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往日情怀 2025-02-13 08:06:41

你不能。签名的积分类型的算术溢出原因​​不确定的行为,不是例外。如果您的程序中发生这种溢出,为时已晚。您无能为力地保存程序。那时它已经处于未定义状态。

在执行乘法之前,您应该检查潜在的溢出

You can't. Arithmetic overflow on signed integral types causes undefined behavior, not an exception. If such overflow happens in your program, it is too late. You can't do anything to save the program. It is already in undefined state at that point.

You should check for potential overflow before you perform the multiplication.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文