如何处理发生故障的析构函数? [英] How can I handle a destructor that fails?

查看:57
本文介绍了如何处理发生故障的析构函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何处理发生故障的析构函数?

How can I handle a destructor that fails?

推荐答案

析构函数从定义上讲不会失败.一旦析构函数运行了对象的死对象,死对象,死对象,并且什么也无法使它再次移动(例如,不同于Java终结器,它可以使对象复活).

这样做的另一个影响是不要将异常抛出析构函数或调用std :: unexpected(IIRC),通常调用std :: terminate会杀死程序.

只是出于兴趣而已...有多种方法可以从析构函数中获取信息,但是如果您诉诸于使用它们,通常(但并非总是如此)意味着您的类设计有问题.问问自己您在可能会失败的析构函数中做什么,以及如果它不是在析构函数中发生,您将如何处理?

干杯,

Ash
Destructors can''t fail by definition. Once the destructor has run the object''s dead, dead, dead and nothing can get it moving again (unlike a Java finaliser, for example, which can resurrect an object).

An additional impact of this is don''t throw exceptions out of a destructor or std::unexpected (IIRC) is called which usually calls std::terminate which kills your program.

Just out of interest... There are ways of getting information out of a destructor but if you resort to using them it usually (but not always) means there''s something wrong with your class design. Ask yourself what you''re doing in a destructor that can fail and what would you do about it if it wasn''t happening in a destructor?

Cheers,

Ash


清理(尽可能多地),记录遇到错误的事实,但不要抛出异常!请参见此处 [
Clean up (as much as you can), log the fact that you got hit the error, but do not throw an exception! See here[^] for details. :)


这篇关于如何处理发生故障的析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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