'终极'辩论 [英] The 'finally' debate

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

问题描述



我刚刚阅读了为什么不开发新的

语言主题中的一些旧文章,并且遇到了最后的辩论。


每当我向C ++程序员提及终于时,我几乎感到情绪化。

回答有关C ++中不需要它的原因。我不明白。


例如,请考虑以下代码。请注意,我只能在当前项目中使用

堆分配的对象(新/删除)。


//

// Foo - 尝试foo。可以抛出一个FooException

//


void Foo()

{

try {

//你的foo业务可能引发FooException

}


catch(...){

//清理你的生意

扔;

}


//清理你的生意

}


现在,最后我可以这样做:


void Foo()

{

尝试{

//你的foo业务可能引发FooException

}


终于{

//清理你的生意

}

}


我发现*很多*比其他例子清洁,因为没有

需要做两次清理。


无论如何,辩论没用,因为我们没有最后。所以我的问题

真的是,人们如何将上述内容重构为更好的东西?


S.


I was just reading through some old articles in the ''Why not develop new
language'' thread and came across the finally debate.

Everytime I mention ''finally'' to C++ programmers I get almost emotional
responses about why it is not needed in C++. I don''t get that.

For example, consider the following code. Please note, I can only use
heap allocated objects in my current project (new/delete).

//
// Foo - Tries to foo. Can throw a FooException
//

void Foo()
{
try {
// Do your foo business that could throw a FooException
}

catch (...) {
// Cleanup your business
throw;
}

// Cleanup your business
}

Now, with finally I could do this:

void Foo()
{
try {
// Do your foo business that could throw a FooException
}

finally {
// Cleanup your business
}
}

Which I find *much* cleaner than the other example as there is no
need to do the cleanup twice.

Anyway, the debate is useless because we don''t have finally. So my question
really is, how do people refactor the above to something nicer?

S.

推荐答案



" Stefan Arentz" < ST *********** @ gmail.com>在消息中写道

news:87 ************ @ keizer.soze.com ...

"Stefan Arentz" <st***********@gmail.com> wrote in message
news:87************@keizer.soze.com...

我当时只是阅读为什么不开发新的语言主题中的一些旧文章,并遇到了最后的辩论。

每次我提到''终于'到C ++程序员我几乎得到了关于C ++中不需要它的原因。我不明白。

例如,请考虑以下代码。请注意,我只能在当前项目中使用
堆分配的对象(new / delete)。

//
// Foo - 尝试foo。可以抛出一个FooException
//

void Foo()
{
尝试{
//你的foo业务可能引发FooException //清理你的业务
扔掉;
}
//清理你的生意
}

现在,终于我可以做到这一点:

无效Foo()
{
尝试{
//你的foo业务可能会引发FooException
}

终于{
//清理你的业务
}
}
<我找到*比其他例子更清洁,因为没有需要两次清理。

无论如何,辩论没用,因为我们没有最后。所以我的
问题确实是,人们如何将上述内容重构为更好的东西?

I was just reading through some old articles in the ''Why not develop new
language'' thread and came across the finally debate.

Everytime I mention ''finally'' to C++ programmers I get almost emotional
responses about why it is not needed in C++. I don''t get that.

For example, consider the following code. Please note, I can only use
heap allocated objects in my current project (new/delete).

//
// Foo - Tries to foo. Can throw a FooException
//

void Foo()
{
try {
// Do your foo business that could throw a FooException
}

catch (...) {
// Cleanup your business
throw;
}

// Cleanup your business
}

Now, with finally I could do this:

void Foo()
{
try {
// Do your foo business that could throw a FooException
}

finally {
// Cleanup your business
}
}

Which I find *much* cleaner than the other example as there is no
need to do the cleanup twice.

Anyway, the debate is useless because we don''t have finally. So my question really is, how do people refactor the above to something nicer?




简单地使用带有析构函数的类(这是因为Java没有

它最终需要的析构函数)


void Foo()

{

FooBusiness biz;

//你的foo业务可能引发FooException

}


FooBusiness析构函数执行清理。这样你甚至不需要

尝试/捕获


john



Simple use a class with a destructor (it''s because Java doesn''t have
destructors that it needs finally)

void Foo()
{
FooBusiness biz;
// Do your foo business that could throw a FooException
}

The FooBusiness destructor does the cleanup. This way you do not even need a
try/catch

john




" Stefan Arentz" < ST *********** @ gmail.com> skrev i en meddelelse

news:87 ************ @ keizer.soze.com ...

"Stefan Arentz" <st***********@gmail.com> skrev i en meddelelse
news:87************@keizer.soze.com...

[snip ]我发现*比其他例子更清洁*因为没有需要两次进行清理。

无论如何,辩论没用,因为我们最终没有。所以我的
问题确实是,人们如何将上述内容重构为更好的东西?

S。
[snip] Which I find *much* cleaner than the other example as there is no
need to do the cleanup twice.

Anyway, the debate is useless because we don''t have finally. So my question really is, how do people refactor the above to something nicer?

S.




嗨Stefan


John Harrison已经回答了你的问题。我只想补充一下

你可以查看一些智能指针的提升。

也是scopeguard更多特别的东西。


/ Peter



Hi Stefan

John Harrison has already answered your question. I just want to add that
you could check out boost for some of the smart pointers there. There is
also "scopeguard" for more special stuff.

/Peter


" John Harrison" <乔************* @ hotmail.com>写道:
"John Harrison" <jo*************@hotmail.com> writes:
简单地使用带有析构函数的类(这是因为Java最终没有最终需要的析构函数)
void Foo()
{FOBBusiness biz;
//你的foo业务可能引发FooException
}

FooBusiness析构函数执行清理。这样你甚至不需要一个
尝试/捕捉
Simple use a class with a destructor (it''s because Java doesn''t have
destructors that it needs finally)

void Foo()
{
FooBusiness biz;
// Do your foo business that could throw a FooException
}

The FooBusiness destructor does the cleanup. This way you do not even need a
try/catch




但是这是怎么回事:


void bar()

{

char * my_ptr;

try

{

SomeObject instace;

my_ptr = new char [987];

//做点什么

instace.doSomehting()//抛出一个异常让我们说MyExec

//使用指针工作

删除my_ptr

}

catch(MyExec & exc)

{

删除my_ptr;

//错误处理

}

}


这里有必要重复代码,另一个例子是文件

处理。

有时候最后 - 阻止可以帮助。


Mayby有人可以告诉我为什么不把它带入C ++


亲切的问候,

Nicolas


-

| Nicolas Pavlidis | Elvis Presly:| \ | __ |

| SE& s的学生KM | 进入goto | \ | __ | |

| pa****@sbox.tugraz.at | ICQ#320057056 | |

| -------------------格拉茨技术大学---------------- |



but what''s about this:

void bar()
{
char * my_ptr;
try
{
SomeObject instace;
my_ptr = new char[987];
// do something
instace.doSomehting() // throws an exception let''s say MyExec
// work with pointer
delete my_ptr
}
catch(MyExec &exc)
{
delete my_ptr;
// error handling
}
}

Here it''s necessary to duplicate code, another example would be file
handling.
Sometimes a finally - block can help.

Mayby someone cann tell me why this was not taken into C++

Kind regards,
Nicolas

--
| Nicolas Pavlidis | Elvis Presly: |\ |__ |
| Student of SE & KM | "Into the goto" | \|__| |
| pa****@sbox.tugraz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|


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

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