Emulating exceptions in C: a case study
sevko.io
sevko.io
But that was interesting read non the less, thanks OP.
>> I think exceptions are very bad idea.
There are many cases, API implementations for example, where I cannot think of a better solution. Packing result codes into returned data, using return values, using global state, using passed state buffers, all those approaches are worse, imo, and lead to less readable and maintainable code. What's your preferred alternative?
In Java/C# I have to use what other people are using :)
My dislike of exceptions (as well as errno and Set/GetLastError) is due their masking, hiding error conditions and situations. And I prefer everything to be stated clearly. Including error handling code (answer to 'did I handled all possible failures of calling this?' should be instant).
Unfortunately proper error handling is hard, nearly as much as 'naming things' :)
Actually, being part of the ISO C standard, they are not platform-specific. Otherwise, just use C++ where destructors take care of cleanup when an exception is thrown.
The standard doesn't define the full semantics, specially what happens when a signal, trap, OS exception and similar occurs.
That's circular reasoning. The objection is that it only works well in certain circumstances. Mostly in fairly simple applications with either small teams or rigid conventions to enforce clarity and prevent bugs.
I don't think it is a circular reasoning. There's always the good and the bad way to use something, even C++ exceptions.
I'm not sure your example is a counterexample either. Implementing part of a fairly simple runtime (like Lua's) using longjmp is an example of a good use of longjmp, in my opinion.
If abstracted away they don't necessarily cause that many problems. Sure, it'd be nicer if there were a better alternative, but often there's just none. And no C++ isn't always an alternative. Many older platforms have only horribly bad C++ compilers, certainly not ones where you want to rely on exception handling.
No they really don't; sometimes you have no choice but to use C because you're using a framework or platform where that's the only option.
This is why you had to push all local variables onto a clearnup list in each an every function, this I think, from memory was a call to CleanupStack::Push with a wrapper macro.
If indeed they did this becuase exxceptions were 'too slow', then the problem, im my option would be that they were using exceptions far too much -- as status indications and so on. They are, again, in my option a great help used in the right way, with the C++ RAII idiom, which is essential in the face of exceptions.
I believe the paper of Wadler makes this most clear. Some time ago, it was a real eye-opener for me:
http://homepages.inf.ed.ac.uk/wadler/papers/essence/essence....