Software Best Practices

Voices on Software Development Best Practices
Welcome to Software Best Practices Sign in | Join | Help
in Search

Error handling in C

Last post 02-20-2008 10:12 PM by Rohit Karlupia. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 11-22-2007 9:40 AM

    Error handling in C

    I have a problem that seems to bite me all the time. I work with C in an embedded environment and it is very important that the code is stable. At the places I have worked the way to ensure this is usually to check all return values all the time. These error return values are then propagated to the caller in a long chain. This however creates what I consider to be very bloated and ugly code. For example:

    -----8<-----
    someStruct* pStruct = NULL;

    result = Operation1( a, b );
    if ( SUCCESS(result) )  {
      pStruct = (someStruct*)malloc( sizeof( someStruct ) );
      if ( pStruct != NULL ) {
        result = Operation2( pStruct, c, d );
      }
    }
    else {
      result = ERROR;
      if ( pStruct ) {
        free( pStruct );
      }
    }

    if ( SUCCESS(result) && pStruct )  {
       result = Operation3( pStruct, e, f );
    }
    else {
      // Some other error handling
    }

    ....

    return result;

    ----->8-----

    What is happening above? I haven't got the slightest. Code gets very difficult to read, understand and maintain. 90% of the effort is spent trying to get the error handling right and 10% on the actual business problem.

    Problems I see with this:

    1) Readabilty goes down. A lot. I is difficult to understand simple stuff. Stuff that is difficult to begin with gets so complex that I cannot understand it.
    2) The code gets harder to maintain.
    3) The risk that an error has been introduced is increased because of the added complexity.
    4) Code is bloated. What probably is a relative simple thing to do has now become complex with all the error handling overhead.
    5) What if something actually goes wrong? It is difficult to simulate failure of various platform functions and therefore there is a good chance that the error handling code for a particular function never actually has been run. If it never have been run there is probably all kinds of errors that could cause a crash.

    Of course there need to be some error handling, I just hope there is some better way to do it in C. How do I get the error handling out of my way as much as possible in order for me to solve the business problems? Is there any solution? Or is this something I have to live with?

    BR,
    Nick

  • 02-20-2008 10:12 PM In reply to

    Re: Error handling in C

    I generally prefer goto's for error handling.

    #define IfTrue(x)  if (!(x)) goto OnError

    Modified code looks as follows:

    int myCode(....) { 

    IfTrue(Operation1( a, b ));

    pStruct = (someStruct*)malloc( sizeof( someStruct ) );

     

    IfTrue(pStruct);

    goto OnSucces; 

    OnError:

        if (pStruct) {

            //cleanup
     

        } 

    OnSuccess: 

        return ...

    -----------------

    If you can write cleanup your mess in the OnError label from any error that happens in the function, you can always jump to OnError on error. You can easily extend the macro to take a error code to return, incase the check fails.

Page 1 of 1 (2 items)
Seminars           www.Construx.com           Consulting