
Exceptions in C with longjmp and setjmp - QUFB
http://www.di.unipi.it/~nids/docs/longjump_try_trow_catch.html
======
Dan_Korn
Unlike with real C++ exceptions, the "finally" block is not entered if a
"return" is used inside the "catch" block. And, of course, in this system,
exceptions can be caught only in the function from which they are thrown,
unlike real C++ exceptions, which can be caught anywhere in the call stack.
But the solution provided here is good, and is probably the best possible one
for C.

------
notacoward
If you ever want to have some "fun" try testing an implementation of this.
Like the authors and many others, I've also gone down this road of
implementing exceptions using longjmp/setjmp. Writing the code was not nearly
as hard as writing tests for every permutation of ignoring, catching,
rethrowing, finally'ing, and so on through multiple levels. Then the _really_
good part: seeing how your implementation interacts with ucontext-based
lightweight threads. I'll leave that one as an exercise for the reader,
because I'm evil.

