About Error handling in C

本文介绍了C语言中标准库和Unix/Linux系统调用提供的错误处理机制。通过使用errno、strerror和perror等函数,可以有效地增强程序的健壮性。文章详细解释了如何判断和处理错误情况,并给出了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C Programming language itself doesn't provide any good mechanism for error
handling. But standard library and Unix/Linux system calls do provide a
convinient way to handle errors. External variable errno, function strerror and
function perror from library <errno.h>, <string.h> and <stdio.h> are all the
things you need to make your programs more robust.
The general way to use errno to handle error is like this:
if ( (fp = fopen( filename, mode )) == NULL ) { fprintf( stderr, "cannot open file %s: %s/n", filename, strerror( errno ) ); return -1; }
if you want to print error messages onto standard output(screen), you can use
perror as an alternative.
perror( const char *msg );
perror( msg ); is equal to printf( "%s: %s/n", msg, strerror( errno ) );
There is a little difference between perror and strerror, the former is more
elegant and easier to use, but it can print messages on standard output only and
its format is predefined which cannot be changed. strerror provides much more
freedom. You can write the message to anywhere and use any format you want.

We know something goes wrong according to the return value of some functions
like fopen etc. Then we can retrieve the detail information of the error through
errno and strerror and output the info to user or do some recovery operations.
But many times, we cannot know abnormal things happened through return value.
fgets, for instance, would return NULL if either EOF reached or error occured.
For these situations, we need to check errno to see whehter error has occured.
Before you do that you have to know two rules about errno: First, its value
is never cleared by a routine if an error does not occur. Therefore, we should
examine its value only when the return value from a function indicates that an
error occurred. Second, the value of errno is never set to 0 by any of the
functions, and none of the constants defined in <errno.h> has a value of 0.
As a result, if you want to know whether there is an error through errno, you'd
better initialize it, then see if it remains its initial value. Like this:
errno = 0; /* some operation which an error might occur */ if ( errno != 0 ) { /* an error has occured */ fprintf( stderr, "error: %s/n", strerror( errno ) ); return -1; }

In addition, if you write some error handling routines for system calls, you'd
better save the original errno. Because, an error could occur during your
handling routine and errno could be overwritten. Take err_doit in comhdr.h for
example:
static void err_doit( int errnoflag, const char *fmt, va_list ap ) { int errno_save; char buf[ MAXLINE ]; errno_save = errno; /* value caller might want printed */ vsprintf( buf, fmt, ap ); if ( errnoflag ) { sprintf( buf+strlen(buf), ": %s", strerror( errno_save) ); } strcat( buf, "/n" ); fflush( stdout ); /* in case stdout and stderr are the same */ fputs( buf, stderr ); fflush( NULL ); /* flushes all stdio output streams */ return; }
Here, vsprintf might fail and modify errno, if so, the following strerror(errno)
will no longer be the original error. Therefore, we have to save a copy of errno
in errno_save, just in case.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值