我可以告诉析构函数是否发生了异常吗? [英] Can I tell in the destructor if an exception occured ?

查看:64
本文介绍了我可以告诉析构函数是否发生了异常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hy


我有一些析构函数可以在程序流程中执行一些功能工作。

问题是析构函数应该只用于清理,因为

异常可能会随时上升,而析构函数只会被调用

清理。


那怎么能我告诉你,从析构函数中,如果调用已经作为正常的控制流程的一部分进行调用,并且析构函数可以发挥其功能

角色,或者如果调用由于异常和

析构函数应该回滚,中止和清理?


如果我需要将此状态传播给其他在当前析构函数返回之前被称为

的析构函数,以便其他析构函数可以采取合适的行动吗?


为什么没有真正的方法可以与析构函数通信?

构造函数我可以选择,我可以传递参数,但是对于析构函数?


谢谢

" Timothy Madden"

罗马尼亚

解决方案

On 2004年11月17日星期三12:47:27 +0200,Timothy Madden

< ba **** @ rmv.spam.home.ro>写道:

Hy

我有一些析构函数可以在程序流程中做一些功能工作。
问题是析构函数应该只用于清理 - 因为
异常可能会随时上升,并且只会要求析构函数进行清理。

那么如何从析构函数中判断出调用已经成为正常控制流程的一部分,析构函数可以发挥其功能性的作用,或者如果调用是由异常引起的,那么析构函数应该是回滚,中止和清理?

如果我需要在当前析构函数返回之前将此状态传播到其他析构函数,以便其他析构函数可以执行适当的行动?

为什么没有真正的方法与析构函数通信?
我可以选择的构造函数,我可以传递参数,但是对于析构函数?
谢谢你
Timothy Madden
罗马尼亚




uncaught_exception()会告诉你。但是很多编译器显然已经直到最近才出现问题,例如: GCC 3.4报告为

处理好了,而早期版本没有。


-

Bob Hairgrove No**********@Home.com




" Bob Hairgrove" <在***** @ bigfoot.com>在消息中写道

news:ei ******************************** @ 4ax.com ...

2004年11月17日星期三12:47:27 +0200,Timothy Madden
< ba **** @ rmv.spam.home.ro>写道:

Hy

我有一些析构函数可以在程序流程中做一些功能工作。
问题是析构函数应该只用于清理 - 因为
异常可能会随时上升,并且只会要求析构函数进行清理。

那么如何从析构函数中判断出调用已经进行了
aspart正常的控制流程,并且析构函数可以发挥其功能
的作用,或者如果调用是由于异常而导致的,那么析构函数应该回滚,中止和清理?

如果我需要将此状态传播到当前析构函数返回之前调用
的其他析构函数,以便其他析构函数可以采取适当的操作?

为什么没有真正的方法可以与析构函数通信?
构造函数我可以选择,我可以传递参数,但对于析构函数

谢谢你
蒂莫西·马登< br / >罗马尼亚



uncaught_exception()会告诉你。但很多编译器显然直到最近才遇到麻烦,例如据报道GCC 3.4处理好了,而早期版本没有。



我在我的平台上试用了它(80x86,Win2000,VC ++ 6.0 SP5),它没有'工作

:(

我能做的其他事吗?

什么?


谢谢你

Timothy Madden

罗马尼亚


2004年11月17日星期三14:02:21 + 0200,Timothy Madden

< ba **** @ rmv.spam.home.ro>写道:

uncaught_exception()会告诉你。但是很多编译器显然直到最近才遇到麻烦,例如GCC 3.4被报告处理好了,而早期的版本却没有。

我在我的平台(80x86,Win2000,VC ++ 6.0 SP5)上尝试过它,它不起作用
:(
我还能做什么?



是的。快速将编译器更新到MSVC ++ 7.1 ...版本6是

在很多方面都被打破了。顺便说一下,您可以从MS网站免费下载命令行版本

,名为MS Visual C ++ Toolkit。缺点

是它们只提供静态库来链接C / C ++
运行时库。但除此之外,它功能齐全,您可以使用代码优化来实现



不幸的是,我从来没有机会使用uncaught_exception

我自己,所以我不能说它是否适用于这个编译器或

不是。


-

Bob Hairgrove
No**********@Home.com


Hy

I have destructors that do some functional work in the program flow.
The problem is destructors should only be used for clean-up, because
exceptions might rise at any time, and destructors will be called for
clean-up only.

So how can I tell, from within the destructor, if the call has been made as
part of normal flow of control and the destructor can play its functional
role, or if the call has been made as a result of an exception and the
destructor should rollback, abort and clean up ?

What if I need to propagate this state to other destructors that are called
before the current destructor returns, so that the other destructors can
take appropiate action ?

How come there is no real way to comunicate with the destructors ?
Constructors I can choose, I can pass parameters to, but for destructors ?

Thank you
"Timothy Madden"
Romania

解决方案

On Wed, 17 Nov 2004 12:47:27 +0200, "Timothy Madden"
<ba****@rmv.spam.home.ro> wrote:

Hy

I have destructors that do some functional work in the program flow.
The problem is destructors should only be used for clean-up, because
exceptions might rise at any time, and destructors will be called for
clean-up only.

So how can I tell, from within the destructor, if the call has been made as
part of normal flow of control and the destructor can play its functional
role, or if the call has been made as a result of an exception and the
destructor should rollback, abort and clean up ?

What if I need to propagate this state to other destructors that are called
before the current destructor returns, so that the other destructors can
take appropiate action ?

How come there is no real way to comunicate with the destructors ?
Constructors I can choose, I can pass parameters to, but for destructors ?

Thank you
"Timothy Madden"
Romania



uncaught_exception() will tell you. But many compilers apparently have
had trouble with it until only recently, e.g. GCC 3.4 was reported to
handle it OK, whereas earlier versions did not.

--
Bob Hairgrove
No**********@Home.com



"Bob Hairgrove" <in*****@bigfoot.com> wrote in message
news:ei********************************@4ax.com...

On Wed, 17 Nov 2004 12:47:27 +0200, "Timothy Madden"
<ba****@rmv.spam.home.ro> wrote:

Hy

I have destructors that do some functional work in the program flow.
The problem is destructors should only be used for clean-up, because
exceptions might rise at any time, and destructors will be called for
clean-up only.

So how can I tell, from within the destructor, if the call has been made aspart of normal flow of control and the destructor can play its functional
role, or if the call has been made as a result of an exception and the
destructor should rollback, abort and clean up ?

What if I need to propagate this state to other destructors that are calledbefore the current destructor returns, so that the other destructors can
take appropiate action ?

How come there is no real way to comunicate with the destructors ?
Constructors I can choose, I can pass parameters to, but for destructors ?
Thank you
"Timothy Madden"
Romania



uncaught_exception() will tell you. But many compilers apparently have
had trouble with it until only recently, e.g. GCC 3.4 was reported to
handle it OK, whereas earlier versions did not.


I tryed it on my platform (80x86, Win2000, VC++ 6.0 SP5), it doesn''t work
:(
Anything else I can do ?
Anything ?

Thank you
"Timothy Madden"
Romania


On Wed, 17 Nov 2004 14:02:21 +0200, "Timothy Madden"
<ba****@rmv.spam.home.ro> wrote:

uncaught_exception() will tell you. But many compilers apparently have
had trouble with it until only recently, e.g. GCC 3.4 was reported to
handle it OK, whereas earlier versions did not.


I tryed it on my platform (80x86, Win2000, VC++ 6.0 SP5), it doesn''t work
:(
Anything else I can do ?



Yes. Update your compiler to MSVC++ 7.1 ... quickly. Version 6 is
broken in many ways. BTW you can download the command-line version for
free from the MS website, called "MS Visual C++ Toolkit". The drawback
is that they only provide static libraries to link with the C/C++
runtime libraries. But otherwise, it is fully functional and you can
use code optimizations.

Unfortunately, I have never had occasion to use uncaught_exception
myself yet, so I cannot say whether it works with this compiler or
not.

--
Bob Hairgrove
No**********@Home.com


这篇关于我可以告诉析构函数是否发生了异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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