Error Handling
The basic error function is simple-error which takes a string and returns it as an exception.
A more useful version is error which, as in Qi II, shares the same syntax as output, except that object
returned is an exception.
Shen includes a basic type secure exception handling mechanism drawn from the
Qi library called trap-error. trap-error receives two arguments, an expression E and a function F. If E
evaluates to normal form E', then E' is returned. If an exception C is raised it is sent to F and (F C)
is returned.
The function error-to-string allows exceptions to be turned into strings and printed or
examined. This function is defined only for exceptions and should return an error for any other type of
object.
Note that trap-error is type secure and its type is A --> (exception --> A) --> A.
Some examples
(trap-error (/ 1 0) (/. E -1)) gives -1 : number
(trap-error (/ 1 0) (/. E (error-to-string E))) gives the error message as a string
"division by zero". |