Exceptions are not about going from one place to another. It's about unwinding the stack properly and calling the destructors of variables on the stack. This is not a close emulation at all.
Emulating exceptions in C: a case study
21–28 of 28 posts
Re: Emulating exceptions in C: a case study
#22I think exceptions are very bad idea. Not nearly as bad as implicit nullability but comparable. Hacky emulations of exceptions... Well I don't want to be near such code. 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?
Re: Emulating exceptions in C: a case study
#23I think exceptions are very bad idea. Not nearly as bad as implicit nullability but comparable. Hacky emulations of exceptions... Well I don't want to be near such code. But that was interesting read non the less, thanks OP.
I agree it was an interesting read. Haven't thought about setjmp and longjmp in many years. >> 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…
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' :)
Re: Emulating exceptions in C: a case study
#24Exceptions are not about going from one place to another. It's about unwinding the stack properly and calling the destructors of variables on the stack. This is not a close emulation at all.
While I'm not saying this is a good idea, you can combine a pool allocator with longjmp exceptions to get an approximation of unwinding. It requires that you are very careful about structuring your code, but I once wrote a whole web server this way: http://git.annexia.org/?p=rws.git;a=tree (using: http://git.annexia.org/?p=c2lib.git;a=tree ).
Re: Emulating exceptions in C: a case study
#25Earlier quoted context omitted.
While I'm not saying this is a good idea, you can combine a pool allocator with longjmp exceptions to get an approximation of unwinding. It requires that you are very careful about structuring your code, but I once wrote a whole web server this way: http://git.annexia.org/?p=rws.git;a=tree (using: http://git.annexia.org/?p=c2lib.git;a=tree ).
But sometimes it's not just memory. It can be files you need to close, or any other resource held.
Re: Emulating exceptions in C: a case study
#26Earlier quoted context omitted.
But sometimes it's not just memory. It can be files you need to close, or any other resource held.
That too can be queued up, with a list of functions to call to free those resources.
Re: Emulating exceptions in C: a case study
#27Earlier quoted context omitted.
While I'm not saying this is a good idea, you can combine a pool allocator with longjmp exceptions to get an approximation of unwinding. It requires that you are very careful about structuring your code, but I once wrote a whole web server this way: http://git.annexia.org/?p=rws.git;a=tree (using: http://git.annexia.org/?p=c2lib.git;a=tree ).
But sometimes it's not just memory. It can be files you need to close, or any other resource held.
Re: Emulating exceptions in C: a case study
#28This 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.