在c ++中尝试/最终实现 [英] try/finally implementation in c++

查看:71
本文介绍了在c ++中尝试/最终实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好


这是我用C ++实现try / finally

构造的一个小小的编程挑战。我已经完成了几个实现,但它们都有

限制。主要尝试使用析构函数执行

finally码。然而,这有一个很大的局限性,即析构函数
无法看到局部变量。另一种解决这个问题的方法

如下:


{

MyBaseException * processedException = NULL;

尝试

{

< try code>

}

catch(MyBaseException& e)

{

processedException = e.Clone();

}


< finally code>


if(processedException)

throw * processedException;

}


但是,这种方法有限制,它只适用于特定的

类异常对象。


我很想看到其他想法


问候

Alex

Hi all

Its one of my little pragramming challenges to implement a try/finally
construct in C++. I have done several implementations but they all have
limitations. Have tried mostly using a destructor to execute the
"finally" code. However this has the big limitation that the destructor
cannot see local variables. Another approach that solves this problem
is the following:

{
MyBaseException* handledException = NULL;
try
{
<try code>
}
catch(MyBaseException& e)
{
handledException = e.Clone();
}

<finally code>

if (handledException)
throw *handledException;
}

However, this method has the limitation that it works only for specific
classes of exception objects.

I''m curious to see other ideas

Regards
Alex

推荐答案



avasilev写道:

avasilev wrote:
大家好

这是我在C ++中实现try / finally
构造的一个小小的编程挑战。我已经完成了几个实现,但它们都有限制。主要尝试使用析构函数来执行
finally。码。然而,这有一个很大的限制,即析构函数无法看到局部变量。
Hi all

Its one of my little pragramming challenges to implement a try/finally
construct in C++. I have done several implementations but they all have
limitations. Have tried mostly using a destructor to execute the
"finally" code. However this has the big limitation that the destructor
cannot see local variables.




不是我推荐它但是......


如果析构函数是包含那些

变量的本地结构的一部分,那就可以了。


需要考虑的事情。



Not that I am recommending it but...

It could if the destructor was part of a local struct containing those
variables.

Something to think about.


avasilev写道:
avasilev wrote:
我很想看到其他想法



news:comp.lang.c ++。moderated在这个主题的

上有一个巨大的多线程对话。有人建议在

语言中添加一个''finally''关键字。


我发现有人认为C ++可以学习一些关于清理的知识来自Java的
非常令人不安。


大多数消息来源推荐RAII用于C ++的异常中立清理机制。


-

Phlip
http://www.greencheese .org / ZeekLand < - 不是博客!!!



news:comp.lang.c++.moderated has a humongous multi-thread conversation on
this topic. Someone has suggested adding a ''finally'' keyword to the
language.

I find the idea that anyone thinks C++ can learn something about cleanup
from Java very disturbing.

Most sources recommend RAII for C++''s exception-neutral cleanup mechanism.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


* avasilev写道,2006年3月31日22:58:
* avasilev wrote, on 31/03/2006 22:58:

我很想看到其他想法

I''m curious to see other ideas




忽略自然语言文本(如果你不懂挪威语)和

专注于链接&代码:


< url:http://utvikling.com/cppfaq/04/04/02/index.html>。


-

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

问:为什么这么糟糕?

A:热门发布。

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



Disregard the natural language text (if you don''t know Norwegian) and
concentrate on links & code:

<url: http://utvikling.com/cppfaq/04/04/02/index.html>.

--
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?


这篇关于在c ++中尝试/最终实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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