我正在使用VC2010,并编写以下代码来测试“set_unexpected”功能。

#include <iostream>
#include <exception>

void my_unexpected_handler()
{
    std::cout << "unexpected handler" << std::endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
    set_unexpected(my_unexpected_handler);

    throw 1;

    return 0;
}

但是,从未调用过“my_unexpected_handler”(该字符串未打印到控制台,我试图在my_unexpected_handler中设置断点,但未遇到)。

我的代码有什么问题?

谢谢

抱歉,我误解了意外的异常。但是,即使我将代码更改为以下代码
#include <iostream>
#include <exception>

void my_unexpected_handler()
{
    std::cout << "unexpected handler" << std::endl;
}

void func() throw(int)
{
    throw 'h';
}

int _tmain(int argc, _TCHAR* argv[])
{
    std::set_unexpected(my_unexpected_handler);

    func();

    return 0;
}

还是不行吗?也就是说,未调用“my_unexpected_handler”。

最佳答案

您可能没有做错任何事情。您正在使用Visual Studio 2010,并且该编译器不支持异常规范。好吧,它将使用它们来语法检查代码,但不会在运行时检查异常类型。

http://msdn.microsoft.com/en-us/library/wfa0edys(v=vs.100).aspx

通常认为这不是问题,因为通常认为异常规范没有用,实际上在c++ 11标准中已弃用

关于c++ - “set_unexpected”在VC2010中不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11068888/

10-11 23:18