Live data from Hacker News

Emulating exceptions in C: a case study

sevko.io

11–20 of 28 posts

Re: Emulating exceptions in C: a case study

#11
post #6
post #4

Earlier quoted context omitted.

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.

> it also works ok when done properly

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.

Re: Emulating exceptions in C: a case study

#12
post #6

Earlier quoted context omitted.

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.

> it also works ok when done properly 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.

Well, Lua coroutines uses longjmp and they're not "fairly simple" in any way.

I don't think it is a circular reasoning. There's always the good and the bad way to use something, even C++ exceptions.

Re: Emulating exceptions in C: a case study

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

> Actually, being part of the ISO C standard, they are not platform-specific.

The standard doesn't define the full semantics, specially what happens when a signal, trap, OS exception and similar occurs.

Re: Emulating exceptions in C: a case study

#14
I actually wrote a JSON parser for fun and profit 2 weeks ago. It's mind blowing to me how his code is similar to mine. I wrote mine completely from scratch. I even called my routine `parseValue`. Once one has a look at the JSON grammar, writing a parser for it is trivial.

Re: Emulating exceptions in C: a case study

#15

Earlier quoted context omitted.

> it also works ok when done properly 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.

Well, Lua coroutines uses longjmp and they're not "fairly simple" in any way. I don't think it is a circular reasoning. There's always the good and the bad way to use something, even C++ exceptions.

If "done properly" implies "works ok", then it's circular reasoning.

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.

Re: Emulating exceptions in C: a case study

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

As if longjmp and c++ exceptions are the only two ways to go. How about return codes? Or error out parameters? Not so bad to work with if you be consistent about the style...

Re: Emulating exceptions in C: a case study

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

> Everybody has access to C++ now.

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.

Re: Emulating exceptions in C: a case study

#19
post #6

Earlier quoted context omitted.

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.

> it also works ok when done properly 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've seen longjmp used in a number of fairly large projects, with diverse teams. E.g. postgresql.

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.

Post reply on HN