3

I'm writing program in C and I have function handleError(int errcode) available. All it does is unallocate all memory space and call exit(errcode).

In program I have 2 options.

  1. Have all functions return error code to caller. Do the full chain of handling returned error codes to main and handle it here.

  2. Have most of functions returning void and call handleError whenever the error occurs and program has to be stoped.

What is more readable, manageable, and performance-wise choice?

Our team is not united in this matter and I need some outside view on this.

EDIT:

I am gonna add few informations. Errors will be rare and none of errors detected that way will be recoverable.

user978734
  • 303
  • 4
  • 12
  • Go for solution 1. In case you might reuse your code one day, or your project grows, you'd sure regret it if having gone for 2. – alk Nov 10 '12 at 13:28
  • "C handling errors" is something of an oxymoron -- there never has been a good way, just different compromises. The best choice (of several poor choices) will depend in large part on the overall style of programming, how often interfaces return error conditions, how detailed you want to be in error reporting, how much you're willing to sacrifice convenience for robustness, etc. – Hot Licks Nov 10 '12 at 13:29
  • 1
    Hint: Consider writing some macros to simplify return code checking, if you go that way. – Hot Licks Nov 10 '12 at 13:30
  • 2
    By the way: http://stackoverflow.com/questions/385975/error-handling-in-c-code (Note both the accepted and top-voted answers) – jonvuri Nov 10 '12 at 13:34

1 Answers1

2

I just posted a somehow related question. I just wanted to mention the UDI way of handling errors:

  • allocate an error structure that you pass down to the functions you call
  • in case of error / warning each function adds its own error to the structure

In this way you may trace the error and see various components reacting to that error.

Bottom line: 1.

Community
  • 1
  • 1
Nicu Tofan
  • 1,052
  • 14
  • 34