previous  next
Catch
Syntax
Catch C EndCatch;
Catch C In E EndCatch;

where C is a sequence of commands and E is a variable identifier.
Summary
catch an error
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 '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.

IMPORTANT NOTE: There is a bug in 'Catch'.  Any 'Return' command used
inside 'Catch' must return some value.  If not, the 'Return' command
will just return from the Catch-EndCatch statement; it will not return from
the function within which the statement is embedded.  There is an
example below.

Example

Define Test(N)
  Catch
    PrintLn(1/N);
  In E EndCatch;
  If Type(E) = ERROR Then Print("An error occurred: ", GetErrMesg(E)) EndIf;
EndDefine;
Test(3);
1/3

-------------------------------
Test(0);

An error occurred: Division by zero
-------------------------------

         --Illustration of the BUG --
Define Test2()
  Catch
    Print('Hello ');
    Return;  -- incorrect: no value is returned
  EndCatch;
  PrintLn('world.');
EndDefine;
Test2();
Hello world.

-------------------------------
Define Test3()
  Catch
    Print('Hello ');
    Return 3;  -- correct a value is returned
  EndCatch;
  PrintLn('world.');
EndDefine;
Test3();
Hello 3
-------------------------------
See also: