News for hackers. Stuff that matters.
Re:Let's write a setjmp by ur-whale on Sunday February 12, 2023 @05:13AM (#34761629)
> "setjmp" and "longjmp" are the mechanism for implementing exceptions in C

Correct, but reductive.

They can be used for many other things.

Re:Let's write a setjmp by adrian_b on Sunday February 12, 2023 @05:39AM (#34761775)
The more general POV is that "setjmp" defines a continuation and "longjmp" invokes it.

While the most frequent use of explicit continuations is for implementing exceptions, you are right that there are many other programming techniques based on explicit continuations.

Re:Let's write a setjmp by rwmj on Sunday February 12, 2023 @06:26AM (#34762007)
I'm now wondering if anyone has written an actual continuation library in C (ie saving and restoring the whole stack). Is it possible?
Re:Let's write a setjmp by yvdriess on Sunday February 12, 2023 @06:57AM (#34762173)
yes and yes.
Re:Let's write a setjmp by menaerus on Sunday February 12, 2023 @08:19AM (#34762609)
Coroutines are one good example.
Re:Let's write a setjmp by someweirdperson on Sunday February 12, 2023 @05:35AM (#34761749)
> They can be used for many other things.

The underlying primitive offers even more flexibility, but exceptions (in the sense of programming languages) can also be used for many other things than exceptions (in the sense of natural language, to handle exceptional cases).

Re:Let's write a setjmp by bogomipz on Sunday February 12, 2023 @06:09AM (#34761925)
>"... but exceptions (in the sense of programming languages) can also be used for many other things than exceptions ..."

Can you elaborate? What are the other ways exceptions are used when not used to handle exceptional situations?