Here’s another post from Tony Aldon. Sacha already covered it in Emacs News but it’s interesting enough that it deserves another mention. The post is about catch/throw and examples of its use.
Aldon starts with some simple made up examples of using the catch/throw paradigm. He shows some of the ways it can be used. Most of the variation is in specifying the symbol that names the catch block but he also shows some examples of nested catch blocks.
After the simple introductory examples, Aldon presents some real examples from the Emacs source code. They’re more complicated, of course, and involve more code than just the catch/throw but they do show catch blocks in the wild.
The main takeaway about catch/throw is that it’s a mechanism for non-local exits—the throw can even be in a separate function—and is not an error mechanism like the try/catch sequence in Python, Javascript, C#, and many other languages. Its quintessential use is to break out of loops. A nice example is a dolist
loop: there’s no way to specify an exit criterion so the catch/throw mechanism is perfect for exiting the loop when some criterion is satisfied.