3 ms·
If you have ever done any non-trivial coding in C, you will have come across the need (or the advantages) of using goto for local error handling within a functi
by etatoby 10y ago
If you have ever done any non-trivial coding in C, you will have come across the need (or the advantages) of using goto for local error handling within a function.
You can write the error handler code for a given function (which usually just resets some resources and returns an appropriate error value) in a single place, usually at the end, but jump to it from within any loop or structure within the function, as soon as you detect that the computation cannot proceed.
setjmp / longjmp is the natural extension of this mechanism to an entire library, as in a set of tightly coupled functions that are hopefully hidden behind a public API. I think it's a good design choice. If done properly it makes the code more readable, more easily maintainable, and more optimized for the most common execution path. I wouldn't avoid using those libraries just for that design choice.