STL的未处理异常 [英] Unhandled exceptions with STL

查看:120
本文介绍了STL的未处理异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

STL C ++具有处理意外异常的方法 [

Hi everyone,

STL C++ has a way of dealing with unexpected exceptions[^] that I would like to mimic. It''s a very interesting feature that I would like to know how to implement.

The question is: how the hell do they do that? I checked the source code and it contains nothing but forward declarations, so I assume the implementation is compiled in the .dll

Anybody?

Regards

推荐答案

这是由编译器实现的.
它是C ++标准的一部分.

当前的Microsoft编译器中未实现它:

http://msdn.microsoft.com/en-us/library/awbt5tew (v = vs.100).aspx [ http://msdn.microsoft.com/en-us/library/awbt5tew (v = vs.110).aspx [
This is implemented by the compiler.

It is part of the C++ standard.

It is not implemented in the current Microsoft compilers:

http://msdn.microsoft.com/en-us/library/awbt5tew(v=vs.100).aspx[^]

But looks like they will implement it soon:

http://msdn.microsoft.com/en-us/library/awbt5tew(v=vs.110).aspx[^]

There is no way to do it except at the compiler level.


即使编译器确实支持异常规范,您也确实不想使用它们.

意外的处理程序对于某个进程是全局的,因此绝对没有顶级异常处理程序不能做的任何事情.没什么-不是只白痴的鸟.实际上,它甚至比顶级异常处理程序还要糟糕,因为不能保证调用当前作用域中所有对象的析构函数.即使您调用exit()从自动对象的代码析构函数中解脱出来,也不会被调用.关于您唯一可以做的明智的事情是(希望)抛出一个异常,该异常是规范允许的类型,并祈祷该异常一直到达主函数或线程函数.

因此,您最好编写:
Even if the compiler does support exception specifications you really don''t want to use them.

The unexpected handler is global for a process, so there''s absolutely nothing it can do that a top level exception handler can''t. Nothing - not a dicky bird. In fact it''s even worse than a top level exception handler because none of the destructors for objects currently in scope are guaranteed to be called. Even if you call exit() to bail out of your code destructors for automatic objects still aren''t called. About the only sensible thing you can do is throw an exception that''s (hopefully) of a type allowed by the specification and pray it reaches all the way to main or the thread function.

So you might as well write:
int main()
try
{
    do_something();
}
catch( std::exception &e )
{
    std::cout << "Something went horribly wrong: " << e.what() << std::endl;
}
catch( ... )
{
    std::cout << "Something went horribly wrong, no idea what!" << std::endl;
}

,因为如果要重新启动,至少您的代码会自行清理并处于确定性状态.

as at least your code will clean up after itself and be in a deterministic state if you want to restart.


如果您使用的编译器是unexpected处理函数,未实现,则可以在应用程序级别提供解决方法.为此,足以在每个线程的堆栈顶部捕获所有异常.在这种情况下,不要在异常处理程序中引发任何异常.

—SA
If you are using a compiler where unexpected handler is not implemented, you can provide a work-around on the application level. For this, it''s enough to catch all exceptions on the top of the stack of each thread. It''s important no to throw any exceptions in the exception handler in this case.

—SA


这篇关于STL的未处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆