Handling errors in C
Programming languages such as C language do not provide direct support for error handling but because they are system program languages, it provides the lowest level of return values. Most functions of C and functions in Unix return values 1 or null in any error case and set an errno error code for global variables and instructions for errors that occur during the function call. You can find many different error codes in the Header file named.
So a C programmer can check the return value and perform the correct action based on the return value. In fact, the programmer should set the value errno to 0 at the time of program initialization. A value of 0 indicates that there is no error in the program.
The function perror () and strerror () and inform errno error in C
The C program language provided with perror () and strerror () functions can be used to display errno error messages.
The perror () function displays the string you pass to it, followed by a colon, a white space, and then the text that describes the current error value.
The strerror () function returns the pointer to the text that represents the error value.
Try to simulate an error condition and try opening a non-existent file. Here I use both functions to show how to use, but you can use one or more ways to print your error value. The second important point to keep in mind is that you should use stderr to generate all errors.
#include #include /* header file de su dung cac ham va hang can thiet*/ #include extern int errno ; int main () { FILE * pf ; int errnum ; pf = fopen ( "unexist.txt" , "rb" ); if ( pf == NULL ) { errnum = errno ; fprintf ( stderr , "Gia tri cua errno la: %dn" , errno ); perror ( "Error duoc in boi ham perror" ); fprintf ( stderr , "Loi xuat hien khi mo file: %sn" , strerror ( errnum )); } else { fclose ( pf ); } printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; }
Compile and execute the above C program to see the results:
Error divided by number 0 in C
This is one of the very common errors during the split process, any programmer who does not check the condition of the number divided by zero may encounter this error during execution.
The code below fixes this error by checking the condition if the dividend is zero before dividing:
#include #include main () { int sochia = 15 ; int sobichia = 0 ; int thuong ; if ( sobichia == 0 ){ fprintf ( stderr , "Ban dang thuc hien phep chia cho so 0!!! Ket thuc chuong trinh .n" ); exit (- 1 ); } thuong = sochia / sobichia ; fprintf ( stderr , "Gia tri cua thuong la : %dn" , thuong ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); exit ( 0 ); }
Compiling and executing the above C program will produce the following results:
Exit status of program in C
In fact to exit the program with EXIT_SUCCESS value in case the program exits after a successful operation. Here EXIT_SUCCESS is a macro defined as value 0.
If you have error conditions in your program, you should exit with a return status of EXIT_FAILURE defined as a value of -1. Now write the above program as follows:
#include #include main () { int sochia = 36 ; int sobichia = 6 ; int thuong ; if ( sobichia == 0 ){ fprintf ( stderr , "Ban dang thuc hien phep chia cho so 0!!! Ket thuc chuong trinh .n" ); exit ( EXIT_FAILURE ); } thuong = sochia / sobichia ; fprintf ( stderr , "Gia tri cua thuong la: %dn" , thuong ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); exit ( EXIT_SUCCESS ); }
According to Tutorialspoint
Previous lesson: Pressing type in C
Next lesson: Recursive in C
You should read it
- errno.h in C
- How to fix 'Diagnosing Your PC' error in Windows 10
- How to fix VPN error 619
- How to fix 403 Forbidden Error
- How to fix A20 Error when starting the computer
- How to fix 408 Request Timeout error
- How to fix FPT Play account error on Smart TV automatically
- How to fix 'This App Can't Run on Your PC' error on Windows 10
May be interested
- Quick method to fix Excel loop errorquick method to fix excel loop errors, handling circular references errors in excel is super simple.
- Washer errors and how to fix themin the process of using the washing machine, you encounter problems such as machine jam, machine does not turn, do not discharge, ... do not rush to call a mechanic to fix it or try ways ...
- Handle Winmail.dat errors in Outlook in 4 waysone of the most common errors on outlook is that the recipient of the message will see an attachment named winmail.dat. we will guide you how to fix it in the article below. please consult.
- Handling Android System User Interface Errorsandroid system ui errors often appear with the status line 'system ui has stopped' or is unresponsive.
- Air conditioning does not work, causes and remediesused for a long time, even if the air conditioner of any company, there will be trouble at times, for example, the phenomenon of air conditioner has a strange smell, there is a sound inside, not receiving remote or air conditioning does not work , ... at that time, we often thought that the air conditioner was damaged and went to a repairman. however, before doing this, please refer to the tips below to know how to handle when the air conditioner is not working.
- How to fix errors cannot be found or missing .DLL filesdll errors are especially troublesome because there are many types of files like this that exist, all of which pose the potential for errors. fortunately, there are several troubleshooting steps you can take to fix any dll errors.
- Common wifi errors, wifi network errors and how to fix themall common wifi errors, wifi network errors and how to fix them are compiled and shared by tipsmake. if you are encountering errors, please refer to this article to handle errors promptly.
- Managing Windows Networks using Script - Part 8: Handling remote scripting errors using Network Monitor 3.0in part 7, we started troubleshooting the error that occurred when trying to remotely change the ip address on an xp computer using the changeipaddress.vbs script that was previously developed.
- Burning with water, a more environmentally friendly method of handling corpses than cremationburning with water or alkaline hydrolysis is a form of eco-friendly rest and much less energy-efficient than cremation and burial.
- How to Configure Sendmailthis how to covers the process of configuring email handling after registering a domain. sendmail is the unix/linux software that does email handling. it's not a mail user agent (mua) like the email programs you'd recognize. it is just a...