zoukankan      html  css  js  c++  java
  • 32、深入理解计算机系统笔记,Unix下处理错误的风格

    1、unix风格的错误机制下,当函数,如wait执行出错后,it returns -1

    and sets the global variable errno to an error code that indicate the cause of the error.如果成功,则返回有用的结果。

    if ((pid = wait(NULL)) < 0) {

    fprintf(stderr, "wait error: %s\n", strerror(errno));

    exit(0);

    }

    The strerror function returns a text description for a particular value of errno

    2、Posix-style

        执行错误时,通过返回的错误码来进行判断。成功则返回0

    if ((retcode = pthread_create(&tid, NULL, thread, NULL)) != 0) {

    fprintf(stderr, "pthread_create error: %s\n", strerror(retcode));

    exit(0);

    }

    3、DNS-style

    These functions return a NULL pointer on failure and set the global h errno variable.

    if ((p = gethostbyname(name)) == NULL) {

    fprintf(stderr, "gethostbyname error: %s\n:", hstrerror(h_errno));

    exit(0);

    }

    4、处理错误的封装函数

    void unix_error(char *msg) /* unix-style error */

    {

    fprintf(stderr, "%s: %s\n", msg, strerror(errno));

    exit(0);

    }

    void posix_error(int code, char *msg) /* posix-style error */

    {

    fprintf(stderr, "%s: %s\n", msg, strerror(code));

    exit(0);

    }

    void dns_error(char *msg) /* dns-style error */

    {

    fprintf(stderr, "%s: %s\n", msg, hstrerror(h_errno));

    exit(0);

    }

  • 相关阅读:
    475. Heaters
    69. Sqrt(x)
    83. Remove Duplicates from Sorted List Java solutions
    206. Reverse Linked List java solutions
    100. Same Tree Java Solutions
    1. Two Sum Java Solutions
    9. Palindrome Number Java Solutions
    112. Path Sum Java Solutin
    190. Reverse Bits Java Solutin
    202. Happy Number Java Solutin
  • 原文地址:https://www.cnblogs.com/mydomain/p/2121902.html
Copyright © 2011-2022 走看看