失败的dtors [英] dtors that fail

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

问题描述

当dtor无法执行任务时,有人可以推荐做什么吗?

Can anyone recommend what to do when a dtor fails to perform its task?

推荐答案

* Angel Tsankov:
* Angel Tsankov:

任何人都可以推荐当dtor无法执行任务时该怎么做?

Can anyone recommend what to do when a dtor fails to perform its task?




这是一个FAQ。


< url:http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.3> ;.


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:Top-发布。

问:usenet和电子邮件中最烦人的事情是什么?



This is a FAQ.

<url: http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.3>.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Angel Tsankov写道:
Angel Tsankov wrote:
任何人都可以推荐当dtor无法执行任务时该怎么办?
Can anyone recommend what to do when a dtor fails to perform its task?




(注意,以下是有效C ++中第8项的解释< br $> b $ b Scott Meyers。)


如果常见问题解答中的推荐令你烦恼,那你就是c采取

方法将责任转移给班级用户,通过

提供一种方法来完成所有可能引发异常的事情。

例如,假设您有一个代表网络连接的类:


类连接

{

socket_type s;

public:

//这里的其他方法


~connection()

{

尝试

{

s.close()//这可能会失败并抛出异常

}

catch(...)

{

//吞下异常,可能写日志条目等。

}

}

};


您可以通过

提供一种贴近的方法,使你的课堂看起来像:


班级连接

{

socket_type s;

bool关闭;

公开:

//这里的其他方法


void close()

{

s.close();

关闭= true;

}


~connection()

{

if(!closed)

{


尝试

{

s.close()//这可能会失败并抛出异常

}

catch(...)

{

//吞下异常,可能写日志条目等。

}

}

}

};


现在的用户该类可以选择调用close()并处理

出现的任何异常,或者允许析构函数吞噬

异常的默认行为。


-Alan



(Note, the following is a paraphrasing of Item 8 from Effective C++ by
Scott Meyers.)

If the recommendation in the FAQ annoys you, then you can take the
approach of shifting the responsibility to the user of the class, by
providing a method that does all the things that can throw exceptions.
For example, let''s say you have a class to represent a network connection:

class connection
{
socket_type s ;
public :
// other methods here

~connection()
{
try
{
s.close() // this could fail and throw an exception
}
catch (...)
{
// swallow exception, possibly write log entry or such.
}
}
} ;

You might instead shift responsibility to the user of the class by
providing a close method, making your class look like:

class connection
{
socket_type s ;
bool closed ;
public :
// other methods here

void close()
{
s.close() ;
closed = true ;
}

~connection()
{
if (!closed)
{

try
{
s.close() // this could fail and throw an exception
}
catch (...)
{
// swallow exception, possibly write log entry or such.
}
}
}
} ;

Now the user of the class has the option to call close() and deal with
any exceptions that arise, or allow the default behavior of having
exceptions swallowed by the destructor.

-Alan


2006年1月24日星期二14:52:33 GMT, al *** @ start.no (Alf P. Steinbach)

写道:
On Tue, 24 Jan 2006 14:52:33 GMT, al***@start.no (Alf P. Steinbach)
wrote:
* Angel Tsankov:
* Angel Tsankov:
任何人都可以建议当dtor无法执行任务时该怎么做?
Can anyone recommend what to do when a dtor fails to perform its task?



这是一个FAQ。
< url:http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.3>。



This is a FAQ.
<url: http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.3>.




那里没有提到最重要的方面。只将代码放入

无法失败的析构函数或与失败无关的析构函数

用于程序的进度。 iostreams是一个很好的例子,其中违反了这条规则。 ofstream刷新并关闭

析构函数中的文件。当close()失败时,数据可能会丢失和/或文件

已损坏。


祝你好运,

Roland Pibinger



The most important aspect is not mentioned there. Put only code into
the destructor that cannot fail or for which failure is not relevant
for the progress of the program. iostreams are a good example where
this rule is violated. ofstream flushes and closes the file in the
destructor. When close() fails data is probably lost and/or the file
is corrupted.

Best regards,
Roland Pibinger


这篇关于失败的dtors的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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