Live data from Hacker News

Emulating exceptions in C: a case study

sevko.io

1–10 of 28 posts

Re: Emulating exceptions in C: a case study

#4
post #2

This is like an article on how to use a screwdriver as a chisel.

If you need a chisel but only have a screwdriver then advice on how to best use it sounds valuable to me.

Everybody has access to C++ now. Even on the Arduino. If you need exceptions, go to C++. "longjmp" was a bad idea when it was first invented, and it hasn't improved with age.

Re: Emulating exceptions in C: a case study

#6
post #4

Earlier quoted context omitted.

If you need a chisel but only have a screwdriver then advice on how to best use it sounds valuable to me.

Everybody has access to C++ now. Even on the Arduino. If you need exceptions, go to C++. "longjmp" was a bad idea when it was first invented, and it hasn't improved with age.

Yet whole applications are written around it and they work perfectly well. Not saying that is an argument that it is somehow a good idea, just that while it might looks bad, it also works ok when done properly.

Re: Emulating exceptions in C: a case study

#8
post #5

Let me use my favourite tool of intellectual terrorism: Just use monads. Ouch, now we have two problems here. :P

To be serious: This was my first thought, too. Exceptions are essentially one kind of monads, and any complex compiler or interpreter could make good use of more types of monads.

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....

Re: Emulating exceptions in C: a case study

#9
> longjmp() and setjmp() are tricky. They’re obscure, can give rise to subtle bugs, are highly platform-specific, and, if abused, will probably lead to awfully confusing code

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.

Re: Emulating exceptions in C: a case study

#10
post #9

> longjmp() and setjmp() are tricky. They’re obscure, can give rise to subtle bugs, are highly platform-specific, and, if abused, will probably lead to awfully confusing code 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.

Perhaps use them in production code only (where performance is critical), and use something else in debug/development mode?
Post reply on HN