如何在C++中捕获数据类型溢出异常。
在这里,基本可能会超出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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能。签名的积分类型的算术溢出原因不确定的行为,不是例外。如果您的程序中发生这种溢出,为时已晚。您无能为力地保存程序。那时它已经处于未定义状态。
在执行乘法之前,您应该检查潜在的溢出。
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.