exception handling in C?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manjuscripts
    New Member
    • Aug 2007
    • 6

    exception handling in C?

    how can I handle exceptions in C?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You use setjump() and longjump().

    It's very ugly.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by weaknessforcats
      You use setjump() and longjump().

      It's very ugly.
      That's in the eye of the beholder; in the seventies this was considered a great
      way to non-local jump in C.

      kind regards,

      Jos (<--- "those were the days" ;-)

      Comment

      • kreagan
        New Member
        • Aug 2007
        • 153

        #4
        Originally posted by manjuscripts
        how can I handle exceptions in C?
        The way my company does it:

        Every function returns a 0 if function was a success or -1 if the function failed.
        If a function was a failure, goto cleanup. Here is some snippets.

        [CODE=c]
        #define try(x) status = (x); \
        if (status < 0){ \
        GOTO CATCH; \
        } \[/CODE]

        Checks the status.

        [CODE=c]status = someFunction();
        try(status); [/CODE]

        Checks if status is ever <0

        [CODE=c]CATCH:
        cleanUpCode(); [/CODE]

        Catch statement.

        If you want, instead of just returning -1, you can return anything less than 0 and assign these values different error messages.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by manjuscripts
          how can I handle exceptions in C?
          C does not produce and handle software exceptions in the same way C++ does i.e. no throw, try and catch keywords.

          You can intercept and handle hardware exceptions produced externally to the program.

          You use can use setjmp and longjump to handle some exceptions, however to catch them you will have to call the signal function and install a signal (exception) handler. There are 5 - 10 things you can catch.

          The only signal I have ever had the need to intercept was floating point errror. This was in a complex maths package. The user selected 2 matrices of chemical elements, these were supposed to match, however if they did not match in some fairly subtle and hard to predetermin ways the resulting matrix calculation produced floatiing point errors (divide by zero IIRC). We used the signal to intercept this error, reset the floating point processor and restart the program (using a longjump) rather than just letting it fall over in a big heap.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by kregan
            The way my company does it:
            That doesn't work. It relies on human infallibility, which does not exist. All it takes is one new hire who wasn't at the meeting and the convention is kaput.

            If the compiler cannot enforece the rule, then the rule is no good. (BTW: this is why naming convenrtions don't work either).

            Comment

            Working...