Dyalog Primer - Error Handling
∇ z←a foo w
 z←a div w
∇ z←a div w;⎕TRAP
 ⎕TRAP←11 'E' '''Goldurn it, broke again'''
2 foo 3
2 foo 0
∇ z←a fooey w;⎕TRAP
 :Trap 11
2 fooey 3
2 fooey 0
- ⎕TRAP is
the long-established error-trapping mechanism; there are a lot more
options than shown here.
- :Trap fits
right in with the control flow concept; what Dyalog don't (often) spell
out is that it's really a mask for ⎕TRAP; you
can see this if you stop fooey
on lines  and .
a stylistic judgement whether code should use error-traps to deal with
application logic or whether it's cleaner to use return codes as part
of function results; different programmers adopt different styles.
Something to bear in mind is that if you use error-trapping
(example - what to do when a file your code wants to open doesn't
exist) to implement application logic you've cut down your options for
dealing with real errors.
code is fine for end users, but can be a debugger's nightmare - always
give yourself a way to make sure errors can be "untrapped" as an
- The clearer code that comes from :Trap is
one argument favouring its use (and by its simplicity it ought to
that seems to happen in the real world is that application users start
talking about "APL errors" - it seems to be something that leaks out of
APL developer discussions in a way that doesn't happen with other
languages. Gets APL a bad name when really it's the coder who
should get a bad name.
- Dyalog User Guide
- Dyalog Language Reference
- Error Messages
- ⎕DM ⎕EM ⎕EN ⎕EXCEPTION ⎕SIGNAL ⎕TRAP
- :TRAP control structure
so that line  manifests a true error (and stops) for the benefit of
code maintenance when there is a DOMAIN ERROR (hint - you'll probably
want to control error behaviour with a global variable).
- Define a dynamic function equivalent to the first version of fooey
- Define a "division" function which is equivalent to ÷ when the arguments are compatible, but returns 1 if they are incompatibly shaped, or 0 if either is non-numeric.
- Define an operator that applies its (single) operand
when possible - returning a two-element result (1 and the result of
applying the operand). If the operand fails it should return a
two-element vector (0 and the diagnostic message).
- Define a function which is equivalent to ÷, but signals a DOMAIN ERROR if the right argument contains any even numbers.