处理习语时出错 [英] Error handling idioms

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

问题描述

我对使用传统嵌套ifs处理函数错误的习惯感兴趣,因为我认为这可能非常尴尬

并且难以维护,当错误检查的数量达到大约

3左右时。它在嵌套循环中也变得非常笨拙,你想要在循环条件下检查正常的循环处理,而不是错误。

是的,你可以放一些通用的退出在循环条件下标记,但是如果你发现错误就完成了你的b $ b,那么简单地完全抛弃所有这些就更加简单了。


例外可行,但这与原则

不一致,例外情况可用于特殊情况,即你的错误

不要指望会发生。


基本上我想到的是使用多次返回的东西

来自一个函数和可能得到的点(两者都不一致)与

传统的结构化编程)。


这对我来说似乎很好 - 想法?更多阅读链接?


bool f()

{

if(错误条件)

转到ERROR_RETURN;


//做东西


for ...

{

for ...

{

//做东西

if(错误条件)

转到ERROR_RETURN;

}

}

if(错误条件)

转到ERROR_RETURN;


返回true;


ERROR_RETURN:

//做一些清理

返回false;

}

I''m interested in an idiom for handling errors in functions without
using traditional nested ifs, because I think that can be very awkward
and difficult to maintain, when the number of error checks gets about
3 or so. It also gets very awkward in nested loops, where you want to
check for normal loop processing in the loop condition, not errors.
Yes, you could put some generic exit flag in the loop condition, but
when you''re simply done if you find an error, it''s much more
straightforward to simply leave all of it.

Exceptions would work, but that is inconsistent with the principle
that exceptions be used for exceptional cases, i.e. errors that you
don''t expect could occur.

Basically what I have in mind is something using multiple return
points from a function and possibly gotos (both inconsistent with
traditional structured programming).

This seems good to me - thoughts? Further links for reading?

bool f()
{
if (error condition)
goto ERROR_RETURN;

// do stuff

for ...
{
for ...
{
// do stuff
if (error condition)
goto ERROR_RETURN;
}
}
if (error condition)
goto ERROR_RETURN;

return true;

ERROR_RETURN:
// do some cleanup
return false;
}

推荐答案

* je ****** @ yahoo.com

我对在没有
使用传统的嵌套ifs,因为我认为这可能非常尴尬

且难以维护,当错误检查的数量达到大约

3或所以。它在嵌套循环中也变得非常笨拙,你想要在循环条件下检查正常的循环处理,而不是错误。

是的,你可以放一些通用的退出在循环条件下标记,但是如果你发现错误就完成了你的b $ b,那么简单地完全抛弃所有这些就更加简单了。


例外可行,但这与原则

不一致,例外情况可用于特殊情况,即你的错误

不要指望会发生。
I''m interested in an idiom for handling errors in functions without
using traditional nested ifs, because I think that can be very awkward
and difficult to maintain, when the number of error checks gets about
3 or so. It also gets very awkward in nested loops, where you want to
check for normal loop processing in the loop condition, not errors.
Yes, you could put some generic exit flag in the loop condition, but
when you''re simply done if you find an error, it''s much more
straightforward to simply leave all of it.

Exceptions would work, but that is inconsistent with the principle
that exceptions be used for exceptional cases, i.e. errors that you
don''t expect could occur.



不要让愚蠢的原则妨碍实用性。

Don''t let silly principles get in the way of practicality.


基本上我的想法是使用多个返回

点来自函数和可能得到的(两者都与

传统的结构化编程不一致)。
Basically what I have in mind is something using multiple return
points from a function and possibly gotos (both inconsistent with
traditional structured programming).



许多C ++专家提倡单入口多出口(SEME),包括Andrei Alexandrescu在内的



有些人(比如我)提出了相反的论点。


但这只是为了充实可能存在的缺点;不要愚蠢

原则妨碍实用性。

Single-entry-multiple-exit (SEME) is advocated by many C++ experts,
including Andrei Alexandrescu.

Some people (e.g. me) have argued to the contrary.

But that''s just to flesh out possible shortcomings; don''t let silly
principles get in the way of practicality.


这对我来说好看 - 想法?
This seems good to me - thoughts?



以下代码绝对是不合适的:它不是例外安全。


使用C ++析构函数进行清理(称为RAII)。


然后只需使用''return''返回。

The below code is absolutely ungood: it''s not exception safe.

Use C++ destructors for cleanup (called RAII).

Then just use ''return'' to return.


更多阅读链接?
Further links for reading?



查看Petru Marginean和Andrei Alexandrescu的范围内容文章。

Look up Petru Marginean and Andrei Alexandrescu''s scope guard article.


bool f()

{

if(错误条件)

转到ERROR_RETURN;


/ /做什么


for ...

{

for ...

{

//做东西

if(错误条件)

转到ERROR_RETURN;

}

}

if(错误条件)

转到ERROR_RETURN;


返回true;


ERROR_RETURN:

//做一些清理

返回false;

}
bool f()
{
if (error condition)
goto ERROR_RETURN;

// do stuff

for ...
{
for ...
{
// do stuff
if (error condition)
goto ERROR_RETURN;
}
}
if (error condition)
goto ERROR_RETURN;

return true;

ERROR_RETURN:
// do some cleanup
return false;
}



-

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

问:为什么这么糟糕?

A:热门发布。

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

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


在3月2日上午9:50,Alf P. Steinbach < a ... @ start.nowrote:
On Mar 2, 9:50 am, "Alf P. Steinbach" <a...@start.nowrote:

>

以下代码绝对是不合适的:它不是例外安全。


使用C ++析构函数进行清理(称为RAII)。
>
The below code is absolutely ungood: it''s not exception safe.

Use C++ destructors for cleanup (called RAII).



这是一个好点。我的实际代码包含在try / catch

块中,我从示例中省略了。我正在阅读RAII

等等,谢谢。

Well that is a good point. My actual code was enclosed in try/catch
blocks, which I left out from the example. I am reading up on RAII
etc., thanks.


3月2日14:43,jeffc。 .. @ yahoo.com" < jeffc ... @ yahoo.comwrote:
On 2 Mar, 14:43, "jeffc...@yahoo.com" <jeffc...@yahoo.comwrote:

我对在没有

使用的函数中处理错误的习语很感兴趣传统的嵌套ifs,因为我认为这可能非常尴尬而且难以维护,当错误检查的数量大约为
3左右时。它在嵌套循环中也变得非常笨拙,你想要在循环条件下检查正常的循环处理,而不是错误。

是的,你可以放一些通用的退出在循环条件下标记,但是如果你发现错误就完成了你的b $ b,那么简单地完全抛弃所有这些就更加简单了。


例外可行,但这与原则

不一致,例外情况可用于特殊情况,即你的错误

不要指望会发生。
I''m interested in an idiom for handling errors in functions without
using traditional nested ifs, because I think that can be very awkward
and difficult to maintain, when the number of error checks gets about
3 or so. It also gets very awkward in nested loops, where you want to
check for normal loop processing in the loop condition, not errors.
Yes, you could put some generic exit flag in the loop condition, but
when you''re simply done if you find an error, it''s much more
straightforward to simply leave all of it.

Exceptions would work, but that is inconsistent with the principle
that exceptions be used for exceptional cases, i.e. errors that you
don''t expect could occur.



什么???那是一个荒谬的原则。什么是你不会错误的错误
预期会发生?如果你不希望出现这种情况,为什么你会担心编写代码来处理它?<​​br />
我知道原则

不时弹出,但我从来没有找到任何人可以解释它意味着什么。


C ++中的异常的好处是它们允许您从代码的其余部分中清楚地分析错误处理代码。

What??? That''s a ludicrous principle. What is an "error you don''t
expect could occur"? If you don''t expect a situation to occur why are
you worrying about writing code to handle it? I know that principle
pops up from time to time but I''ve never yet found anyone who can
explain what it means.

The benefit of exceptions in C++ is that they allow you to cleanly
separate error handling code from the rest of your code.


基本上我想到的是使用多个返回

点来自函数和可能得到的东西(两者都与

传统的结构化编程不一致)。
Basically what I have in mind is something using multiple return
points from a function and possibly gotos (both inconsistent with
traditional structured programming).



多个回报点 - 也许。毕竟,只要预期是可能的,单一进入多次退出是给定的。 Gotos - 不需要

就像你使用它们一样。

Multiple return points - maybe. After all, as soon as expections are a
possibility, single entry-multiple exit is a given. Gotos - not needed
in the way you are using them.


这对我来说似乎很好 - 想法?阅读的其他链接?
This seems good to me - thoughts? Further links for reading?



对我来说看起来不太好。

Doesn''t look good to me.


>

bool f()

{

if(错误条件)

转到ERROR_RETURN;
>
bool f()
{
if (error condition)
goto ERROR_RETURN;



这里只返回false。对于多个返回点,失败的预先存在是完全可以接受的
的情况。当然,除非前提条件完全在程序员的控制之下(例如,不依赖于

用户输入),在这种情况下,失败的前提条件就是简单一个bug,所以一个

断言可能更合适。

Just return false here. Failed precoditions are a perfectly acceptable
case for multiple return points. Unless, of course, the preconditions
are entirely under the control of the programmer (e.g. don''t depend on
user input) in which case a failed precondition is simply a bug, so an
assert might be more appropriate.


//做的东西


for ...

{

for ...

{

//做东西
if(错误条件)

转到ERROR_RETURN;

}

}
// do stuff

for ...
{
for ...
{
// do stuff
if (error condition)
goto ERROR_RETURN;
}
}



如果循环没有通过for语句隐含的迭代次数运行
,它看起来更像是循环而不是

for循环对我来说。

If the loop does not neceesarily run for the number of iterations
implied by the for statement, it looks more like a while loop than a
for loop to me.


if(错误条件)

转到ERROR_RETURN;


返回true;


ERROR_RETURN:

//做一些清理
if (error condition)
goto ERROR_RETURN;

return true;

ERROR_RETURN:
// do some cleanup



做一些清理永远不应该是必要的。所有需要

清理的资源都应由RAII对象拥有,无论函数何时何地退出,它都会自动清理


"do some cleanup" should NEVER be necessary. All resources that need
cleaning up should be owned by RAII objects which will do the cleaning
up automatically regardless of how and when the function exits.


返回false;
return false;



Gavin Deane

Gavin Deane


这篇关于处理习语时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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