title: Is there a better way to do C style error handling?
body: <p>I'm trying to learn C by writing a simple parser / compiler.  So far its been a very enlightening experience, however coming from a strong background in C# I'm having some problems adjusting - in particular to the lack of exceptions.</p>
  
  <p>Now I've read <a href="http://blogs.msdn.com/b/oldnewthing/archive/2005/01/14/352949.aspx" rel="noreferrer">Cleaner, more elegant, and harder to recognize</a> and I agree with every word in that article; In my C# code I avoid throwing exceptions whenever possible, however now that I'm faced with a world where I <em>can't</em> throw exceptions my error handling is completely swamping the otherwise clean and easy-to-read logic of my code.</p>
  
  <p>At the moment I'm writing code which needs to <em>fail fast</em> if there is a problem, and it also potentially deeply nested - I've settled on a error handling pattern whereby "Get" functions return NULL on an error, and other functions return <code>-1</code> on failure.  In both cases the function that fails calls <code>NS_SetError()</code> and so all the calling function needs to do is to clean up and immediately return on a failure.</p>
  
  <p>My issue is that the number of <code>if (Action() &lt; 0) return -1;</code> statements that I have is doing my head in - it's very repetitive and completely obscures the underlying logic.  I've ended up creating myself a simple macro to try and improve the situation, for example:</p>
  
  <pre><code>#define NOT_ERROR(X)    if ((X) &lt; 0) return -1
  
  int NS_Expression(void)
  {
      NOT_ERROR(NS_Term());
      NOT_ERROR(Emit("MOVE D0, D1\n"));
  
      if (strcmp(current-&gt;str, "+") == 0)
      {
          NOT_ERROR(NS_Add());
      }
      else if (strcmp(current-&gt;str, "-") == 0)
      {
          NOT_ERROR(NS_Subtract());
      }
      else
      {
          NS_SetError("Expected: operator");
          return -1;
      }
      return 0;
  }
  </code></pre>
  
  <p>Each of the functions <code>NS_Term</code>, <code>NS_Add</code> and <code>NS_Subtract</code> do a <code>NS_SetError()</code> and return <code>-1</code> in the case of an error - its <em>better</em>, but it still feels like I'm abusing macros and doesn't allow for any cleanup (some functions, in particular <code>Get</code> functions that return a pointer, are more complex and require clean-up code to be run).</p>
  
  <p>Overall it just feels like I'm missing something - despite the fact that error handling in this way is supposedly easier to recognize, In many of my functions I'm really struggling to identify whether or not errors are being handled correctly:</p>
  
  <ul>
  <li>Some functions return <code>NULL</code> on an error</li>
  <li>Some functions return <code>&lt; 0</code> on an error</li>
  <li>Some functions never produce an error</li>
  <li>My functions do a <code>NS_SetError()</code>, but many other functions don't.</li>
  </ul>
  
  <p>Is there a better way that I can structure my functions, or does everyone else also have this problem?</p>
  
  <p>Also is having <code>Get</code> functions (that return a pointer to an object) return <code>NULL</code> on an error a good idea, or is it just confusing my error handling?</p>