当整数被零除时,msvc 6 会抛出什么异常?

发布于 2024-08-14 19:18:16 字数 479 浏览 7 评论 0原文

我做了一些实验,发现当整数除以零时会引发异常。

#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 技术交流群。

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

发布评论

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

评论(4

瞳孔里扚悲伤 2024-08-21 19:18:16

这是一个 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.

め七分饶幸 2024-08-21 19:18:16

本文声称有一种方法可以使用 _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

罗罗贝儿 2024-08-21 19:18:16

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?

何处潇湘 2024-08-21 19:18:16

在 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.

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