Thread: Example of Error Handling

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    112

    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.

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

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    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.
    Code:
    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) {
          DisplayError(state);
          exit(state);
       }
       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 Cprogramming.com
    Replies: 0
    Last Post: 04-29-2002, 03:56 AM

Tags for this Thread