Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Error Handling

C Error Handling

C does not provide direct support for error handling, but as a systems programming language it lets you access low-level data through return values. When an error occurs, most C or UNIX function calls return 1 or NULL and set an error code errno, which is a global variable indicating that an error occurred during the function call. You can find a wide variety of error codes in the errno.h header file.

Therefore, C programmers can check the return value and then decide what appropriate action to take based on it. Developers should set errno to 0 during program initialization, which is a good programming practice. A value of 0 means there are no errors in the program.

errno, perror(), and strerror()

C provides the perror() and strerror() function to display the errno related text messages.

Let us simulate an error condition by trying to open a file that does not exist. You can use several ways to output error messages; here we use a function to demonstrate the usage. Also note that you should use the stderr file stream to output all errors.

Example

#include

<

stdio.h

#include

<

errno.h

#include

<

string.h

extern

int

errno

;

int

main

(

)

{

FILE

pf

;

int

errnum

;

pf

=

fopen

(

"

unexist.txt

"

,

"

rb

"

)

;

if

(

pf

==

NULL

)

{

errnum

=

errno

;

fprintf

(

stderr

,

"

Error number: %d

\

n

"

,

errno

)

;

perror

(

"

Output error via perror

"

)

;

fprintf

(

stderr

,

"

Error opening file: %s

\

n

"

,

strerror

(

errnum

)

)

;

}

else

{

fclose

(

pf

)

;

}

return

0

;

}

When the code above is compiled and executed, it produces the following result:



错误号: 2

通过 perror 输出错误: No such file or directory

打开文件错误: No such file or directory

Division by zero error

When performing division, if you do not check whether the divisor is zero, it will cause a runtime error.

To avoid this, the following code checks whether the divisor is zero before performing the division:

Example

#include

<

stdio.h

#include

<

stdlib.h

int

main

(

)

{

int

dividend

=

20

;

int

divisor

=

0

;

int

quotient

;

if

(

divisor

==

0

)

{

fprintf

(

stderr

,

"

Divisor is 0, exiting...

\

n

"

)

;

exit

(

1

)

;

}

quotient

=

dividend

/

divisor

;

fprintf

(

stderr

,

"

The value of the quotient variable is: %d

\

n

"

,

quotient

)

;

exit

(

0

)

;

}

When the code above is compiled and executed, it produces the following result:



除数为 0 退出运行...

Program exit status

Normally, when a program finishes an operation successfully and exits normally, it carries the value EXIT_SUCCESS. Here, EXIT_SUCCESS is a macro defined as 0.

If an error condition exists in the program, when you exit the program it carries the status value EXIT_FAILURE, defined as -1. So the above program can be written as:

Example

#include

<

stdio.h

#include

<

stdlib.h

int

main

(

)

{

int

dividend

=

20

;

int

divisor

=

5

;

int

quotient

;

if

(

divisor

==

0

)

{

fprintf

(

stderr

,

"

Divisor is 0, exiting...

\

n

"

)

;

exit

(

EXIT_FAILURE

)

;

}

quotient

=

dividend

/

divisor

;

fprintf

(

stderr

,

"

The value of the quotient variable is: %d

\

n

"

,

quotient

)

;

exit

(

EXIT_SUCCESS

)

;

}

When the code above is compiled and executed, it produces the following result:



quotient 变量的值为 : 4