error - Maple Programming Help

error

error return from a procedure

 Calling Sequence error msgString, msgArg1, msgArg2, ...

Parameters

 msgString - text of error message msgArg1, msgArg2, ... - parameters to substitute into msgString

Description

 • The error statement raises an exception. Execution of the current statement sequence is interrupted, and the block and procedure call stack is popped until either an exception handler is encountered and the exception is caught, or execution returns to the top level.
 • If an exception is not caught, then an error message is displayed. This will be of the form, "Error, (in procName) message", where procName is the name of the procedure in which the error was raised, and message is constructed from msgString and the msgArgs as described below.
 • The msgString is a string value that is independent of any expressions that are to be part of the message (for instance, the string complaining about an unassigned variable would not mention the variable by name). The string can, however, contain numbered parameters of the form %n where n is an integer.
 • The msgArg1, msgArg2, ... arguments are one or more arbitrary Maple expressions that are substituted for the numbered parameters in the msgString in the event that the exception is printed as an error message.
 • If no msgString is given, error raises the most recently occurring exception.
 • The error statement evaluates its arguments, and then creates an exception object, which is an expression sequence with the following elements:
 – The name of the procedure in which the exception was raised (hereinafter referred to as the procName).  If the exception was raised at the top level, the constant 0 appears here.
 – The msgString.
 – The msgArgs, if any.
 • The created exception object is assigned to the global variable lastexception as an expression sequence. The msgString and msgArg sequence is also assigned to lasterror as a symbol (for backward compatibility).
 • In the msgString, numbered parameters are used as placeholders for actual values or names.  For example, the error "f has a 2nd argument, x, which is missing" is specified by the msgString "%1 has a %-2 argument, %3, which is missing", and the msgArgs f, 2, and x.
 • Each numbered parameter consists of the percent symbol, "%", optionally followed by a minus sign, "-", followed by one or more digits making up an integer n. At message display time, the nth msgArg is substituted for the corresponding numbered parameter.
 • A numbered parameter of the form %n displays the nth msgArg in lprint (1D) notation.
 • A numbered parameter of the form %-n displays the nth msgArg, assumed to be a Maple integer, in ordinal form. For example, the value 2 is displayed as "2nd".
 • The special parameter, %0, displays all the msgArgs, separated by a comma and space.
 • The special parameter, %^, appearing at the beginning of the error message, reassigns the blame for the exception to the procedure that called the one that raised the exception. The "%^" will be removed from the message before the exception is raised.
 • The special parameter, %=, appearing at the beginning of the error message, reassigns blame to a procName specified by an extra argument between the msgString and msgArgs. The "%=" will be removed from the message and the procName argument removed from the argument sequence before the exception is raised.
 • You can use StringTools[FormatMessage] to produce the string produced from the lastexception object.
 • For consistency, the Maple WARNING function also uses a msgString and msgArgs.
 • Exceptions can be caught by using the try...catch facility.  The traperror function has been deprecated.
 • In Maple V Release 5.1 and earlier, the mechanism to raise an exception was the ERROR function. This facility has been deprecated.
 • A call to the ERROR function is semantically equivalent to the execution of the error statement, with a few exceptions, outlined below.
 • If called with a first argument that is a string or a symbol containing numbered positional parameters, ERROR works exactly the same way as error. The same is true if ERROR is called with a single argument that is a string or a symbol.
 • If called with multiple arguments, the first of which is a string or symbol, but without positional parameters, then ERROR(a, b, c) is equivalent to error "a, %0", b, c. In other words, the first argument becomes a msgString with ", %0" appended to it, and the remaining arguments become msgArgs for this msgString.
 • If called with any number of arguments, the first of which is not a string or symbol, then ERROR(a, b, c) is equivalent to error "%0", a, b, c. In other words, the msgString "%0" is implied, and all the arguments become msgArgs.
 • In all cases, lasterror is assigned exactly the arguments passed to ERROR, and lastexception is assigned as though the equivalent (as described in the preceding paragraphs) error statement had been executed.
 • The semantics of error have been carefully designed to remain backward compatible with the use of ERROR in Maple V Release 5.1 and earlier.

 • The error statement is thread-safe as of Maple 15.

Examples

 > f := proc (x) if x<0 then error "invalid x: %1", x else x^(1/2) end if end proc:
 > $f\left(-3\right)$
 > $\mathrm{lastexception}$
 ${f}{,}{"invalid x: %1"}{,}{-3}$ (1)
 > $\mathrm{StringTools}:-\mathrm{FormatMessage}\left({\mathrm{lastexception}}_{2..-1}\right)$
 ${"invalid x: -3"}$ (2)
 > $\mathbf{try}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}f\left(-3\right);\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{printf}\left("%s","no error occurred"\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{catch}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}"invalid x":\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{printf}\left("%s","error occurred"\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{catch}:\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{error}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end try}$
 error occurred
 > $\mathrm{lastexception}$
 ${f}{,}{"invalid x: %1"}{,}{-3}$ (3)