Go to the first, previous, next, last section, table of contents.


catch

Control-struct: catch{ ... [ | ... ]throw }
file: job.t
package: muf
status: alpha

When writing nontrivial code, one sometimes needs to abort a fairly deeply nested set of function calls.

One way of handling this problem is by having each function return a flag saying "yes, I'm aborting," or "no, I'm not aborting," and then having every function-call check this flag an conditionally propagate the abort. However, code written this way is tedious to code, ugly to read, and prone to errors. Code with the single purpose of implementing a non-local exit winds up scattered all through the program, which is poor separation of concerns and just plain poor software design.

C solves this problem via setjmp() and longjmp(). Lisp implements a similar but more sophisticated utility via catch and throw, recently adopted by ANSI Forth. Muq implements a similar catch/throw pair:

anyconst catch{ ... [ anyargs | anyconst ]throw ... }

Catch accepts one anyconst argument: It only responds to ]throws given a matching anyconst value.

(This makes selective ]throwing possible, in which a ]throw bypasses several catch clauses to reach a preferred one deeper down on the execution stack.)

Catch then executes the brace-enclosed clause; If the clause complete normally, catch returns a nil value on top of an empty block.

If a matching ]throw is executed in the clause, catch pushes a true value on top of the block returned by ]throw.


Go to the first, previous, next, last section, table of contents.