Perfect Combination of error handling: perror & amp; exit

Source: Internet
Author: User

There are two useful functions for library function error handling: perror and exit: 1. Error Report

The perror function reports errors in a simple and unified manner. Many library functions in ansi c, especially I/O functions, call the operating system to perform some work. But you need to know that there is a possibility of an error in the operating system at any time. For example, if a program tries to read a non-existing disk file, the operating system prompts "error !", Nothing else can be done. At this time, the library function will first save the error code that represents the specific cause of the Operation error to the global integer variable errono (in errno. h), and then it sends the error message to the user program.

The perror function only reports specific errors represented by errno to users. Its prototype is defined in stdio. h as follows:

Perror (* message );

If the message is not NULL and points to a non-NULL string, perror prints the message first, followed by a colon ":" and a space. Finally, the error cause information of the current errno is printed.

Note: When the database function is successfully executed, the errno value is not modified; The errno value is modified only when the function fails to be executed. This means that we cannot test errno to determine whether a function execution error has occurred. Check the value of errno only when the function is correct.

Ii. End execution

Another very useful function is exit, which is used to end the execution of a program. Its prototype is defined in stdlib. h as follows:

Exit (status );

The exit function returns the parameter status to the operating system, and the status indicates whether the program is completed normally. It returns the same value as the value in the main function. There are two predefined symbols about status: EXIT_SUCCESS and EXIT_FAILURE, which indicate the success and failure respectively.

The exit function is particularly useful when an error condition is found to prevent the program from continuing to execute. Generally, after calling the perror function, we call the exit function.

Note: The exit function never returns any value. When exit ends, the program disappears. Where can I return.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.