3 ms·
Here's the error handling I teach for C: http://c.learncodethehardway.org/book/learn-c-the-hard-waych21.html http://c.learncodethehardway.org/book/learn-c-the-
by zedshaw 14y ago
Here's the error handling I teach for C:
http://c.learncodethehardway.org/book/learn-c-the-hard-waych21.html http://c.learncodethehardway.org/book/learn-c-the-hard-waych...
It's using the gotos, but I make macros that hide the actual word "goto" away so that idiots (who think goto is bad because of a ranting "paper" written decades past) will not notice it.
That and I print out error diagnostics, the location of the error at the time, and the errno. These macros probably saved me a good decade of time finding errors in C.
- cube13 14y ago>That and I print out error diagnostics, the location of the error at the time, and the errno. These macros probably saved me a good decade of time finding errors in C. Line number and file macros are freaking awesome. Out of everything on that page those should be the major takeaway for the reader. I also think it would be a good idea to add some context to why the goto is so useful in those examples. If any of the checks fail, you're in a full-stop, stick-a-fork-in-us-we're-done error state. Since you know that the function's operations have failed, and that you should be cleaning up everything immediately, goto is the right choice.
- beagle3 14y agogcc/g++ has had __FUNCTION__ and __PRETTY_FUNCTION__ since forever (the first is just the function name; the 2nd in c++ has the argument types, class name and more) - might be a useful addition to your toolkit. It's easy to trace back from line numbers to file/function etc, but I find that the function name is actually more informative for me -- and is easily incorporated into macros such as yours at no extra cost. C++11 and the latest C have standard equivalents for these, but I'm too lazy to look them up right now..