6.2 Catching errors

On the other hand a symbolic program might want to react to an exception which occurred in a calculation branch. REDUCE supports this by encapsulation with the routines errorset* and errorset . You have to construct the action to be encapsulated as a subroutine call in prefix LISP syntax which is identical to algebraic form syntax: a list where the first element is the name of the function (usually a quoted symbol) followed by the arguments; these must be tagged by quote, best using the function mkquote. The second parameter describes the error mode: if t, an eventual error during the evaluation will be reported to the user, if nil the error message will be suppressed. errorset additionally has a third parameter which tells the error manager whether to print a backtrace or not in an error case. The result of these functions will be

You should use the function errorp for testing whether the call failed or not; in the positive case pick out the result by a car access. Example:

  begin scalar u,v;  
       .  .  .  
   q := errorset!*({’quotsq,mkquote(u),mkquote(v)},nil);  
   if errorp q then rederr "division failed" else  
       q:=car q;  
       .  .  .

Note that you cannot write the expression to be encapsulated in REDUCE syntax here. And also (quotsq u v) would be incorrect because then u and v were constants and not references to the local variables u and v. Only the mkquote expressions guarantee that at run time a list is generated where the values of u and v are inserted as parameters for the call.