(转)linux下C编程错误捕获函数

来源:互联网 发布:teamviewer端口被屏蔽 编辑:程序博客网 时间:2024/04/28 14:17

本文转载自——武特学长的博客——博客地址:http://www.edsionte.com(里面还有很多的好文章,希望大家能多学习)


看了好一段Linux下的C编程,很多东西都有了一定的了解。但是就是有一点比较含糊,那就是Linux下c的错误处理。里面有很多系统函数,也有一些是自己编写的错误处理函数my_err()。就光这点,把我弄迷糊了。今天小组开会时,他们给我说,武特学长博客上面有这方面的讲解。下来看了下,确实收获很多。现在赶紧将这篇文章转载到我的博客上,希望能有更多的人学习。在这里,也谢谢武特学长。

 

————————————————————————————————————————————————————————————————

本文中的错误是指在代码编译完全正确程序可运行的情况下,因为没有成功调用程序中的某些系统调用函数而产生的错误。往往这些系统调用函数通过返回值(比如1,0,-1)来说明其是否调用成功,而程序员需要知道详细的错误信息,因此自建错误捕获函数很有必要。

(1)errno和strerror()

errno它是一个整形的错误代码。当发生错误的时候,系统自动将错误代码赋给errno。使用下面的方法可以获得具体的错误描述:

01void my_err(int error)
02{
03    printf("error:  %s with errno: %d/n",strerror(error),error);
04    exit(1);
05}
06 
07int main()
08{
09            ..............
10        my_err(errno);
11        ..............
12 
13}

其中char *strerror(int errnum);是通过errnum来获取错误描述,errnum即所传递的errno。该函数末尾的exit(1)使得程序发生错误时退出。但应该包含库函数stdlib.h。

下面进行测试,测试程序(源代码在本文末尾。)使用open()函数创建文件,因为要创建的文件已存在,而且使用了O_EXCL参数,因此open()会产生错误。结果如下:

1edsionte@edsionte-laptop:~/code$ ./error
2error:   File exists with errno: 17

该方法可以详细显示错误信息以及错误代码。但不能显示错误出现的行数。

(2)perror()

其函数原型为:void perror(const char *s)。s一般是函数名。该函数会先将函数名打印出来,然后再打印出错误信息。错误信息与errno相对应。第二个参数__LINE__是一个宏,表示当前的行数。使用方法:

01void my_err2(const char* err_string,int line)
02{
03    fprintf(stderr,"error:  line:%d ",line);
04    perror(err_string);
05        exit(1);
06}
07}
08 
09int main()
10{
11            .................
12        my_err2("open",__LINE__);
13                ................
14 
15}

测试结果如下:

1edsionte@edsionte-laptop:~/code$ ./error
2error:  line:29 open: File exists
3}

该方法可以显示错误信息以及错误出现的行数。

以上方法是在《linux C编程》中常用的方法,我适当的作了小调整。现在将这两种方法结合起来:

01void my_err3(const char* err_string,int line,int error)
02{
03    printf("error:  line:%d %s():%s with errno:%d/n",line,err_string,strerror(error),error);
04        exit(1);
05}
06 
07int main()
08{
09            ................
10        my_err3("open",__LINE__,errno);
11            ................
12 
13}

测试结果如下:

1edsionte@edsionte-laptop:~/code$ ./error
2error:  line:30 open():File exists with errno:17

这样就可以显示错误代码,错误描述,错误出现的行数以及出现错误的函数。对于和我一样的新手来说,这里特别要注意的是宏__LINE__前后的那个横线是两个连续的下划线,而不是_LINE_,否则会出现错误。
源代码如下:
说明:本程序只作测试用,为了同时显示三种错误捕获函数的信息,因此屏蔽了每个函数的exit(1)。另外本文头文件函数用“”是因为显示问题,没有什么特别意义。

view source
print?
01#include "errno.h"
02#include "fcntl.h"
03#include "unistd.h"
04#include "stdlib.h"
05#include "stdio.h
06#include "sys/types.h"
07#include "sys/stat.h"
08#include "string.h"
09 
10void my_err(int error)
11{
12    printf("error:  %s with errno: %d/n",strerror(error),error);
13//  exit(1);
14}
15void my_err2(const char* err_string,int line)
16{
17    fprintf(stderr,"error:  line:%d ",line);
18    perror(err_string);
19//  exit(1);
20}
21void my_err3(const char* err_string,int line,int error)
22{
23    printf("error:  line:%d %s():%s with errno:%d/n",line,err_string,strerror(error),error);
24//      exit(1);
25}
26int main()
27{
28    int fd;
29    if((fd=open("example_test.c",O_CREAT|O_EXCL,S_IRUSR|S_IWUSR))==-1)
30    {
31        my_err(errno);
32        my_err2("open",__LINE__);
33        my_err3("open",__LINE__,errno);
34    }
35    close(fd);
36    return 0;
37}

原创粉丝点击