Thread: Example of Error Handling

  1. #1
    Registered User
    Join Date
    Dec 2015

    Example of Error Handling

    I've been doing some reading on error handling, and I tend to use something like this at work when I'm working with various pieces of equipment, sockets, etc.

    I would think this is probably outdated, and my approach could be improved quite a bit.

    Should I start using inline calls. We use C99 at work, but I would like to move to C11 in my next project. They usually allow me lots of liberties in what I do as long the customer is happy with the end product.

    #include <stdlib.h>#include <stdio.h>
    #define ErrChk(func) error = func; if(error != SIMPLE_SUCCESS) {goto Error;}
    enum SimpleErrorCases
    int myErrorTest(void);
    int getHandle(void);
    int getHandle()
        // simulate and error
        return SIMPLE_FAILURE;
    int myErrorTest()
        int error;
        if(error != SIMPLE_SUCCESS)
            printf("There was an error.\n");
        return error;
    int main()
        int error = 0;
        return 0;

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    The best advice is check your compiler's documentation and see what it says about inline. If you use GCC at work, then yes, please inline. Apparently you could have been doing that for a long time now. Inline - Using the GNU Compiler Collection (GCC)

    As far as your control flow example goes, I'm not so sure that's outdated. Inline functions would only give you more flexibility I think.
    inline void DisplayError(int error) {
       const static char *error_str[] = { "There was an error.", "Other error.", "Yet another error.\n" };
        fprintf(stderr, "%s\n", error_str[error]);
    inline int GetHandleOrDie(HandleType **handle) {
       int state = GetHandle(*handle);
       if (state != SIMPLE_SUCCESS) {
       return state;
    Last edited by whiteflags; 07-07-2016 at 03:03 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error handling[c]
    By cmay in forum C Programming
    Replies: 4
    Last Post: 06-17-2009, 04:03 PM
  2. need help on error handling.
    By broli86 in forum C Programming
    Replies: 9
    Last Post: 06-19-2008, 11:55 AM
  3. Error handling...
    By XSquared in forum C Programming
    Replies: 5
    Last Post: 07-13-2003, 03:57 AM
  4. Error handling
    By mepaco in forum C++ Programming
    Replies: 12
    Last Post: 09-06-2002, 10:45 AM
  5. Add Error handling to the FAQ
    By Magos in forum A Brief History of
    Replies: 0
    Last Post: 04-29-2002, 03:56 AM

Tags for this Thread