错误处理 - 你会怎么写? [英] Error handling - How would you write it?
问题描述
这是一个高度自以为是的问题,但我想知道人们在这件事情中会想到什么。
初始化/创建群组时对象组合我通常
巩固错误处理如下:
if(foo_init(& f)== -1 ||
(b = bar_new())== NULL ||
some_function(a,b,c)== -1){
ERR(errno) );
bar_del(b);
返回-1;
}
小号条件这不是太糟糕但有时它可以得到
非常难看。您是否愿意看到这样扩展如下:
if(foo_init(& f)== -1){
ERR(errno);
返回-1;
}
if((b = bar_new())== NULL){
ERR( errno);
返回-1;
}
if(some_function(a,b,c)== -1){
ERR(错误);
bar_del(b);
返回-1;
}
或者你会怎么写这个以及为什么?
迈克
注意:在这个列表中我用_new来表示一个函数,它分配必须释放的
资源而不是_init。另外
ERR可以被认为是跟踪错误状态以进行调试的宏。
目的。
这两个例子(缩小和扩展)是不一样的 - 如果
foo_init()失败......
然后你在" b"上调用bar_del()还没有初始化...
(好吧,你可以把它初始化为0并在bar_del()中处理它)
我会做一个如果(foo_init()|| bar_new())失败;
和bar_del()
marek
" Michael B Allen" < MB ***** @ ioplex.com>在消息中写道
新闻:pa **************************** @ ioplex.com ... < blockquote class =post_quotes>这是一个高度自以为是的问题,但我想知道人们在这个问题上的想法。
当我一起初始化/创建一组对象时我通常
巩固错误处理如下:
if(foo_init(& f)== -1 ||
(b = bar_new())== NULL | |
some_function(a,b,c)== -1){
ERR(errno);
bar_del(b);
返回-1;
}
对于小条件,这不是太糟糕,但有时它会变得非常难看。您是否愿意看到这样扩展如下:
if(foo_init(& f)== -1){
ERR(errno);
返回-1;
}
if((b = bar_new())== NULL){
ERR(错误);
返回-1;
}
if(some_function( a,b,c)== -1){
ERR(错误);
bar_del(b);
返回-1;
}
>或者你会怎么写这个以及为什么?
迈克
注意:在这个清单中我使用_new来表示一个分配必须释放的资源的函数而_init则没有。还可以将ERR视为跟踪错误状态以进行调试的宏。
2005年7月9日星期六20: 09:10 +0200,mm写道:
这两个例子(缩小和扩展)不一样 - 如果
foo_init()失败了...
然后你在b上调用bar_del()还没有初始化...
好吧,让我们说b首先被初始化为NULL而bar_del只返回
一个错误如果b为NULL。
Mike
那么,我会用你的例子 - 一切都在一起if()因为
它更简单更短......
可能因为透明度我会定义非零返回值
表示错误然后你可以写:
if(foo_init()||(b = bar_init())|| sample_function()){
失败;
消息;
}
删除不必要的比较和括号,只生成代码
难以理解......
marek
" Michael B Allen" < MB ***** @ ioplex.com>在消息中写道
新闻:pa **************************** @ ioplex.com ... < blockquote class =post_quotes> 2005年7月9日星期六20:09:10 +0200,mm写道:
这两个例子(收缩和扩展)是不一样的 - 如果
foo_init()失败......
然后你在b上调用bar_del()尚未初始化...
好吧,假设b首先初始化为NULL,如果b为NULL,则bar_del只返回错误。
Mike
This is a highly opinionated question but I''d like to know what people
think in this matter.
When initializing / creating a group of objects together I usually
consolidate the error handling like the following:
if (foo_init(&f) == -1 ||
(b = bar_new()) == NULL ||
some_function(a, b, c) == -1) {
ERR(errno);
bar_del(b);
return -1;
}
With small conditionals this isn''t too bad but sometimes it can get
pretty ugly. Would you rather see this expanded like:
if (foo_init(&f) == -1) {
ERR(errno);
return -1;
}
if ((b = bar_new()) == NULL) {
ERR(errno);
return -1;
}
if (some_function(a, b, c) == -1) {
ERR(errno);
bar_del(b);
return -1;
}
Or how would you write this and why?
Mike
Note: In this listing I use _new to indicate a function that allocates
resources that must be released as opposed to _init which does not. Also
ERR can be considered a macro that tracks error state for debugging
purposes.
Hi,
those two examples (shrinked and expanded) are not the same - what if
foo_init() failes...
then you call bar_del() on "b" which is not initialized yet...
(ok, you can initialize it to 0 before and handle it in bar_del())
I would do a hybrid:
if( foo_init() || bar_new()) FAILED;
if( some_function()) FAILED; and bar_del()
marek
"Michael B Allen" <mb*****@ioplex.com> wrote in message
news:pa****************************@ioplex.com...This is a highly opinionated question but I''d like to know what people
think in this matter.
When initializing / creating a group of objects together I usually
consolidate the error handling like the following:
if (foo_init(&f) == -1 ||
(b = bar_new()) == NULL ||
some_function(a, b, c) == -1) {
ERR(errno);
bar_del(b);
return -1;
}
With small conditionals this isn''t too bad but sometimes it can get
pretty ugly. Would you rather see this expanded like:
if (foo_init(&f) == -1) {
ERR(errno);
return -1;
}
if ((b = bar_new()) == NULL) {
ERR(errno);
return -1;
}
if (some_function(a, b, c) == -1) {
ERR(errno);
bar_del(b);
return -1;
}
Or how would you write this and why?
Mike
Note: In this listing I use _new to indicate a function that allocates
resources that must be released as opposed to _init which does not. Also
ERR can be considered a macro that tracks error state for debugging
purposes.
On Sat, 09 Jul 2005 20:09:10 +0200, mm wrote:
Hi,
those two examples (shrinked and expanded) are not the same - what if
foo_init() failes...
then you call bar_del() on "b" which is not initialized yet...
Well let''s say b is initialized to NULL first and bar_del just returns
an error if b is NULL.
Mike
well, then i would use your example - everything together in one if() since
it is simplier and shorter...
maybe because of transparency i would define that non-zero return value
means error and then you can write just:
if( foo_init() || (b = bar_init()) || sample_function()) {
failed;
message;
}
to remove unnecessary comparisons and brackets which only make code
unreadable...
marek
"Michael B Allen" <mb*****@ioplex.com> wrote in message
news:pa****************************@ioplex.com...On Sat, 09 Jul 2005 20:09:10 +0200, mm wrote:Hi,
those two examples (shrinked and expanded) are not the same - what if
foo_init() failes...
then you call bar_del() on "b" which is not initialized yet...
Well let''s say b is initialized to NULL first and bar_del just returns
an error if b is NULL.
Mike
这篇关于错误处理 - 你会怎么写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!