为什么要将错误处理留给调用者? [英] Why leave the error handling to the caller?

查看:40
本文介绍了为什么要将错误处理留给调用者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在The C Programming Langauge一书中的第163页的顶部。通过K&

R,他们有以下内容:


char * strdup(char * s)

{

char * p;

p =(char *)malloc(strlen(s)+1);

if(p!= NULL)

strcpy(p,s):

返回p;

}


然后他们继续说


" strdup传递该值,将错误处理留给其调用者


为什么要将错误处理留给它'打电话的?没有人会像


char * strdup(char * s)

{

char * p;

if(p =(char *)malloc(strlen(s)+1)== NULL)

exit(1);

else

strcpy(p,s):

返回p;

}

Chad

解决方案

Chad写道:


在The C一书的第163页的顶部编程Langauge通过K&

R,他们有以下内容:


char * strdup(char * s)

{

char * p;

p =(char *)malloc(strlen(s)+1);

if(p!= NULL)

strcpy(p,s):

返回p;

}


然后他们继续说


" strdup传递该值,将错误处理留给其调用者


为什么要将错误处理留给它'打电话的?没有人会像


char * strdup(char * s)

{

char * p;

if(p =(char *)malloc(strlen(s)+1)== NULL)

exit(1);

else

strcpy(p,s):

返回p;

}


Chad



退出程序不是唯一的可能性。

函数无法知道一般情况是什么,并且不能< br $> b $ b决定什么是最好的事情。


对于初学者来说,我不喜欢我冒着图书馆惯例的风险

退出我的程序,我没有任何可能避免这个

或采取其他行动。


Chad说:


在The C Programming Langauge一书的第163页上。通过K&

R,他们有以下内容:


char * strdup(char * s)

{

char * p;

p =(char *)malloc(strlen(s)+1);

if(p!= NULL)

strcpy(p,s):

返回p;

}



更好:


#include< stdlib.h>

#include< string.h>


char * dupstr(const char * s)

{

size_t len = strlen(s)+ 1;

char * p = malloc(len );

if(p!= NULL)

{

memcpy(p,s,len);

}

返回p;

}


>

他们然后继续说


" strdup传递该值,将错误处理留给其调用者


为什么要留下错误处理它的来电者?



因为做出那种

决定不是图书馆功能的工作。库函数应该做它应该做的事情或者

解释为什么它不能,*没有别的*。决定异常结束

计划不是图书馆制作的。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。




" Chad" < cd ***** @ gmail.comwrote in message

news:11 ********************** @ p77g2000hsh。 googlegr oups.com ...


在The C Programming Langauge一书的第163页上。通过K&

R,他们有以下内容:


char * strdup(char * s)

{

char * p;

p =(char *)malloc(strlen(s)+1);

if(p!= NULL)

strcpy(p,s):

返回p;

}


然后他们继续说


" strdup传递该值,将错误处理留给其调用者


为什么要将错误处理留给它'打电话的?没有人会像


char * strdup(char * s)

{

char * p;

if(p =(char *)malloc(strlen(s)+1)== NULL)

exit(1);

else

strcpy(p,s):

返回p;

}



对于safemalloc()库函数来说,有一个很好的例子,即
在失败时终止并显示错误消息。但是我们没有它。


另一个问题是stderr的故意破坏。微软这样做。调用

执行,但不会显示错误消息。这意味着stderr在便携式例程中无用




更多现代语言有异常处理,这是最好的解决方案,

在右手中。程序将以错误消息终止,除非

调用者捕获异常。然而在实践中,没有经验的人经常会做出异常的异常,通常是通过捕获太多太低,或者抛出像负根误差这样的奇怪球。他们的意思是非法

参数,或者有时候将它们用于非错误流控制。


因此我们处理内存处理不足条件通过释放和

返回错误代码,这通常意味着调用者必须达到相同的状态,直到最终控制达到可以访问
用户界面。

-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm


On to top of page 163 in the book "The C Programming Langauge" by K &
R, they have the following:

char *strdup(char *s)
{
char *p;
p=(char *)malloc(strlen(s)+1);
if( p != NULL)
strcpy(p,s):
return p;
}

They then go on to say

"strdup passes that value on, leaving error-handling to its caller"

Why leave the error-handling to it''s caller? Couldn''t a person go like

char *strdup(char *s)
{
char *p;
if (p=(char *)malloc(strlen(s)+1) == NULL)
exit(1);
else
strcpy(p,s):
return p;
}
Chad

解决方案

Chad wrote:

On to top of page 163 in the book "The C Programming Langauge" by K &
R, they have the following:

char *strdup(char *s)
{
char *p;
p=(char *)malloc(strlen(s)+1);
if( p != NULL)
strcpy(p,s):
return p;
}

They then go on to say

"strdup passes that value on, leaving error-handling to its caller"

Why leave the error-handling to it''s caller? Couldn''t a person go like

char *strdup(char *s)
{
char *p;
if (p=(char *)malloc(strlen(s)+1) == NULL)
exit(1);
else
strcpy(p,s):
return p;
}
Chad

Exiting the program is not the only possibility here.
The function has no way to know what the general context is, and can''t
decide what is the best thing to do.

For starters, I would NOT like that I risk that a library routine
exits my program without any possibility for me to avoid this
or to take an alternative course of action.


Chad said:

On to top of page 163 in the book "The C Programming Langauge" by K &
R, they have the following:

char *strdup(char *s)
{
char *p;
p=(char *)malloc(strlen(s)+1);
if( p != NULL)
strcpy(p,s):
return p;
}

Better:

#include <stdlib.h>
#include <string.h>

char *dupstr(const char *s)
{
size_t len = strlen(s) + 1;
char *p = malloc(len);
if(p != NULL)
{
memcpy(p, s, len);
}
return p;
}

>
They then go on to say

"strdup passes that value on, leaving error-handling to its caller"

Why leave the error-handling to it''s caller?

Because it is not a library function''s job to make that kind of
decision. A library function should do what it is supposed to do or
explain why it can''t, and *nothing else*. The decision to abend a
program is not the library''s to make.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.



"Chad" <cd*****@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...

On to top of page 163 in the book "The C Programming Langauge" by K &
R, they have the following:

char *strdup(char *s)
{
char *p;
p=(char *)malloc(strlen(s)+1);
if( p != NULL)
strcpy(p,s):
return p;
}

They then go on to say

"strdup passes that value on, leaving error-handling to its caller"

Why leave the error-handling to it''s caller? Couldn''t a person go like

char *strdup(char *s)
{
char *p;
if (p=(char *)malloc(strlen(s)+1) == NULL)
exit(1);
else
strcpy(p,s):
return p;
}

There quite a strong case for a safemalloc() library function that does
terminate with an error message on fail. However we don''t have it.

Another problem is vandalism of stderr. Microsoft do this. The call
executes, but no error message appears. Which means that stderr is useless
in portable routines.

More modern languages have exception handling, which is the best solution,
in the right hands. The program will terminate with an error message, unless
the caller catches the exception. However in practise inexperienced people
often make a real hash of exceptions, usually by catching too many too low,
or throwing oddballs like "negative root error" where they meant "illegal
argument", or sometimes by using them for non-error flow control.

So we are stuck with handling out of memory conditions by freeing and
returning an error code, which usually means that caller has to to the same,
until eventually control reaches a high level routine which has access to
the user interface.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


这篇关于为什么要将错误处理留给调用者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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