error
error return from a procedure
Calling Sequence
Parameters
Description
Thread Safety
Examples
error msgString, msgArg1, msgArg2, ...
msgString
-
text of error message
msgArg1, msgArg2, ...
arguments to substitute into msgString
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.
For details on the construction of error %n parameter forms (also supported by WARNING and StringTools:-FormatMessage), refer to Error and Warning Message Formatting.
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. The msgString and msgArg sequence are also assigned to lasterror (for backward compatibility).
If an exception occurs within a procedure for which source file and line number information is present, and the corresponding source file can be found, the global variable lasterrorlocus is set to a list consisting of the source file name (as a string), the line number, and the value true if the exception was produced by an error statement or false if it arose some other way (for example, an array indexing error or a division by zero).
If lasterrorlocus is set, invoking the showsource function with no arguments will display the source code around the line on which the error occurred. Likewise, editsource with no arguments will open the file in a text editor and position the cursor on the offending line.
If an error is invoked with multiple arguments, and the first argument does not contain %-style formatting, the error is restructured in one of two ways:
If the first argument is a name or string, an error of the form error "a", b, c is transformed 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 the first argument is not a name or string (for example, a function call), then error f(a), b, c is transformed to error "%0", f(a), b, c. In other words, the msgString "%0" is implied, and all the arguments become msgArgs.
When one of these transformations is applied, only the error applied to lastexception is updated. The content of lasterror is left as it was first set by the error.
Exceptions can be caught by using the try...catch facility. The traperror function has been deprecated.
Instead of raising an exception, the StringTools:-FormatMessage function can be called to produce a character string with such a message. Calling FormatMessage with the argument lastexception[2..] will produces the string version of the message corresponding to the last error that was produced.
In Maple V Release 5.1 and earlier, the mechanism to raise an exception was the ERROR function. This facility has been deprecated, but a call to the ERROR function is semantically equivalent to the execution of the error statement.
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.
For more information on thread safety, see index/threadsafe.
f := proc (x) if x<0 then error "invalid x: %1", x else x^(1/2) end if end proc:
f(-3);
Error, (in f) invalid x: -3
lastexception;
f,invalid x: %1,−3
StringTools:-FormatMessage( lastexception[2..-1] );
invalid x: -3
try f(-3): printf("%s\n","no error occurred") catch "invalid x": printf("%s\n","error occurred") catch : error end try;
error occurred
See Also
lasterror
Procedures
return
StringTools:-FormatMessage
try
WARNING
Download Help Document