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
catch
.
If an error occurs during the execution of C, then it is captured by
the command
catch
and (in the second form) assigned to the variable
E. If no error occurs, then E will contain the value
Null
. Note
the use of the function
GetErrMesg
in the example below.
/**/ deg(zero(R));
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: Expecting type RINGELEM, but found type STRING
error(MyDegError);
^^^^^^^^^^^^^^^^^
|