up previous next
try

try command sequence, catch any errors

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

Description
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.

Example
-- /**/  deg(zero(R));  --> !!! ERROR !!! as expected
-- 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);
1
/**/  MyDeg(zero(R));
-123456
-- /**/  MyDeg("a"); --> !!! ERROR !!! as expected
ERROR: Expecting type RINGELEM, but found type STRING
        error(MyDegError);
        ^^^^^^^^^^^^^^^^^

See Also