up previous next

try and catch an error

Try C1 UponError E Do C2 EndTry
  where C1, C2 are sequences of commands and E is a variable identifier.

Usually, when an error occurs during the execution of a command, the error is automatically propagated out of the nesting of the evaluation. This can be prevented with the use of Try..UponError .

If an error occurs during the execution of the commands C1, then it is captured by the command UponError and assigned to the variable E, and the commands C2 are executed; the string inside E may be retrieved using GetErrMesg . If no error occurs then the variable E and the commands C2 are ignored.

-- /**/  deg(zero(R));  --> !!! ERROR !!!
-- ERROR: Non-zero RingElem required
--  deg(zero(R));
--  ^^^^^^^^^^^^

/**/  Define MyDeg(F)
/**/    Try
/**/      D := Deg(F);
/**/      Return D;
/**/    UponError E Do
/**/      MyDegError := GetErrMesg(E);
/**/      If "Non-zero RingElem required" IsIn MyDegError Then
/**/        Return -123456;
/**/      Else
/**/        error(MyDegError);
/**/      EndIf;
/**/    EndTry;
/**/  EndDefine;

/**/  MyDeg(x);
/**/  MyDeg(zero(R));
-- /**/  MyDeg("a"); --> !!! ERROR !!!
ERROR: Expecting type RINGELEM, but found type STRING

See Also