Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    C Error Handling

    Error handling features are not supported by C programming, which is known as exception handling in C++ or in other OOP (Object Oriented Programming) languages. However, there are few methods and variables available in C's header file error.h that is used to locate errors using return values of the function call. In C, the function return NULL or -1 value in case of any error, and there is a global variable errno that sets the error code/number. Hence, the return value can be used to check errors while programming.

    By principle, the programmer is expected to prevent the program from errors that occur in the first place, and test return values from functions.

    C language provides the following functions to represent errors. These are:
    • perror(): This function returns a string to pass to it along with the textual representation of the current errno value.
    • strerror(): This function is defined in string.h library and this method return a pointer to the string representation of the present errno value.

    Let's see an example to deal with an error situation in C:

    Example:

    //Divided By zero Error i.e. Exception
    #include <stdio.h>
    #include <stdlib.h>
    
    void main() {
       int ddend = 60;
       int dsor = 0;
       int q;
    
       if( dsor == 0){
          fprintf(stderr, "Division by zero! Exiting...\n");
          getch();
          exit(-1);
       }
       q = ddend / dsor;
       fprintf(stderr, "Value of quotient : %d\n", q);
       getch();
       exit(0);
    }


    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.