当整数被零除时,msvc 6 会抛出什么异常?
我做了一些实验,发现当整数除以零时会引发异常。
#include <iostream>
#include <stdexcept>
using namespace std;
int main
(
void
)
{
try
{
int x = 3;
int y = 0;
int z = x / y;
cout << "Didn't throw or signal" << endl;
}
catch (std::exception &e)
{
cout << "Caught exception " << e.what() << endl;
}
return 0;
}
显然它没有抛出 std::exception。它还可能扔什么?
I have been doing a bit of experimenting, and have discovered that an exception is being thrown, when an integer divide by zero occurs.
#include <iostream>
#include <stdexcept>
using namespace std;
int main
(
void
)
{
try
{
int x = 3;
int y = 0;
int z = x / y;
cout << "Didn't throw or signal" << endl;
}
catch (std::exception &e)
{
cout << "Caught exception " << e.what() << endl;
}
return 0;
}
Clearly it is not throwing a std::exception. What else might it be throwing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个 Windows 结构化异常,与 C++ 无关 - 如果它是 C 程序,您会得到相同的异常。
It's a Windows structured exception, which has nothing to do with C++ - you would get the same exception if it were a C program.
本文声称有一种方法可以使用 _set_se_translator 功能。
http://www.codeproject.com/KB/cpp/seexception.aspx
This article claims to have a way to convert a structured exception to a C++ exception using the _set_se_translator function.
http://www.codeproject.com/KB/cpp/seexception.aspx
结果未定义,您可以使用 __try / __ except 块捕获错误(结构化异常处理)。但是,为什么不在除法之前简单地检查错误呢?
The result is undefined, you could use __try / __except block to catch the error (structured exception handling). However, why not simply check for the error before your division?
在 msvc6 中,您可以使用 catch(...) 捕获它并使用 throw 重新抛出它;但是,由于您无法以这种方式检测异常类型,因此您最好做其他事情。
In msvc6 you can catch it with catch(...) and rethrow it with throw; however since you can't detect exception type that way you're better off doing something else.