Check | report "hard" errors |
Assert | signal "soft" custom error |
DumpErrors, ClearErrors | simple error handlers |
IsError | check for custom error |
GetError, ClearError | custom errors handlers |
CurrentFile, CurrentLine | show current file and line of input |
Check(predicate,"error text") |
"error text" -- string to print on error
A "soft" error reporting facility that does not stop the execution is provided by the function Assert.
In> [Check(1=0,"bad value"); Echo(OK);] In function "Check" : CommandLine(1) : "bad value" |
Note that OK is not printed.
Assert("str", expr) pred Assert("str") pred Assert() pred |
"str" -- string to classify the error
expr -- expression, error object
Unlike the "hard" error function Check, the function Assert does not stop the execution of the program.
The error object consists of the string "str" and an arbitrary expression expr. The string should be used to classify the kind of error that has occurred, for example "domain" or "format". The error object can be any expression that might be useful for handling the error later; for example, a list of erroneous values and explanations. The association list of error objects is currently the global variable ErrorTableau.
If the parameter expr is missing, Assert substitutes True. If both optional parameters "str" and expr are missing, Assert creates an error of class "generic".
Errors can be handled by a custom error handler in the portion of the code that is able to handle a certain class of errors. The functions IsError, GetError and ClearError can be used.
Normally, all errors posted to the error tableau during evaluation of an expression should be eventually printed to the screen. This is the behavior of prettyprinters DefaultPrint, Print, PrettyForm and TeXForm; they call DumpErrors after evaluating the expression.
In> Assert("bad value", "must be zero") 1=0 Out> False; In> Assert("bad value", "must be one") 1=1 Out> True; In> IsError() Out> True; In> IsError("bad value") Out> True; In> IsError("bad file") Out> False; In> GetError("bad value"); Out> "must be zero"; In> DumpErrors() Error: bad value: must be zero Out> True; |
In> IsError() Out> False; In> DumpErrors() Out> True; |
DumpErrors() ClearErrors() |
ClearErrors is a trivial error handler that does nothing except it clears the tableau.
IsError() IsError("str") |
GetError("str") ClearError("str") |
GetError returns the error object if a custom error of class "str" has been reported using Assert, or False if no errors of this class have been reported.
ClearError("str") deletes the same error object that is returned by GetError("str"). It deletes at most one error object. It returns True if an object was found and deleted, and False otherwise.
In> x:=1 Out> 1; In> Assert("bad value", {x,"must be zero"}) x=0 Out> False; In> GetError("bad value") Out> {1, "must be zero"}; In> ClearError("bad value"); Out> True; In> IsError() Out> False; |
CurrentFile() CurrentLine() |
These functions are most useful in batch file calculations, where there is a need to determine at which line an error occurred. One can define a function
tst() := Echo({CurrentFile(),CurrentLine()}); |