错误处理相关函数

错误处理

errno由libc定义的全局变量,每个错误原因对应一个错误码,errno在头文件errno.h中声明,是一个整型变量,都是正整数(系统错误号都是正整数)。

Its  value  is  significant only  when  the  return value of the call indicated an error (i.e., -1 from most system calls; -1 or
NULL from most library functions); a function that succeeds is allowed to change errno.

函数返回值与errno是两个不同概念,通过函数返回值判断函数是否成功执行,其可为任意值(正数或负数均可,最好不要为0,系统默认函数成功返回0)。

当函数执行错误时错误号才有意义,指定错误的原因。

若函数返回值就是指定错误号的除外,此时就不能用perror(),要用strerror())

Valid error numbers are all nonzero; errno is never set to zero by any system call or library  function.

errno不会为0,系统调用或库函数绝不会使errno=0,自定义函数最好也不要这么做。

errno is thread-local; setting it in one thread does not affect its value in any other thread.
errno是线程全局变量,一个线程的值不会影响其他线程中errno。

可参考:man errno; man errno.h

  • perror

NAME

perror - print a system error message

SYNOPSIS

#include <stdio.h>

void perror(const char *s);

#include <errno.h>

const char *sys_errlist[];

int sys_nerr;

int errno;

The routine perror() produces a message on the standard error output,describing the last error encountered during a call to a system or library function.

  • strerror

NAME

strerror, strerror_r - return string describing error number

SYNOPSIS #include <string.h>

char *strerror(int errnum);

DESCRIPTION

The strerror() function returns a pointer to a string that describes the error code passed in the argument errnum, possibly using the LC_MESSAGES part of the current locale to select the appropriate language. This string must not be modified by the application, but may be modified by a subsequent call to perror(3) or strerror(). No library function will modify this string.

RETURN VALUE

The strerror() and the GNU-specific strerror_r() functions return the appropriate error description string, or an "Unknown error nnn" message if the error number is unknown.

函数返回静态内存的指针。有些函数的错误码并不保存在errno中,而是通过返回值返回,此时就不能调用perror(),需要调用strerror().

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

int main(int argc, char **argv)
{
    if(argc < 2){ 
        printf("usage: %s no
", argv[0]);
        exit(1);
    }   

    char *errstr = NULL;
    
    errstr = strerror((int)*argv[1]);
    if(errstr == NULL){
        printf("strerror return NULL
");
    } else {
        printf("%s
", errstr);
    }   
    perror("result");

    return 0;
}
  • fprintf
int fprintf(FILE *stream, const char *format, ...);

fprintf(stderr, "Usage: %s outfile
", argv[0]);
  • exit

NAME


exit - cause normal process termination


SYNOPSIS


#include <stdlib.h>


void exit(int status);


The exit() function causes normal process termination and the value of status & 0377 is returned to the parent (see wait(2)).


All open stdio(3) streams are flushed and closed. Files created by tmpfile(3) are removed.


The C standard specifies two constants, EXIT_SUCCESS and EXIT_FAILURE, that may be passed to exit() to indicate successful or unsuccessful termination, respectively.


RETURN VALUE


   The exit() function does not return.
原文地址:https://www.cnblogs.com/embedded-linux/p/5003398.html