error return from a procedure - Maple Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Programming : Flow Control : error

error - error return from a procedure

Calling Sequence

error msgString, msgParam1, msgParam2, ...

Parameters

msgString

-

text of error message

msgParam1, msgParam2, ...

-

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, or execution returns to the top level (in which case the exception becomes an error).

• 

The msgString is a string value that is independent of any parameters that are to be part of the message (for instance, the string complaining about an unassigned variable must not mention the variable by name). The string can, however, contain numbered parameters of the form '%n' where n is an integer.

• 

The msgParam1, msgParam2, ... are one or more arbitrary Maple objects 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 msgParams, if any.

• 

The created exception object is assigned to the global variable lastexception as an expression sequence. The msgString and msgParam 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 msgParams 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 msgParam is substituted for the corresponding numbered parameter.

• 

A numbered parameter of the form %n displays the nth msgParam in lprint notation.

• 

A numbered parameter of the form %-n displays the nth msgParam, 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 msgParams, separated by a comma and space.

• 

You can use StringTools[FormatMessage] to format the string produced from the lastexception value.

• 

For consistency, the Maple WARNING function also uses a msgString and msgParams.

• 

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 next.

• 

If called with a first argument, that is, a string or a symbol containing a 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 parameter becomes a msgString with "`, %0" appended to it, and the remaining parameters become msgParams 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 msgParams.

• 

In all cases, lasterror is assigned exactly the parameters 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.

Thread Safety

• 

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

• 

For more information on thread safety, see index/threadsafe.

Examples

f := proc (x) if x<0 then error "invalid x: %1", x else x^(1/2) end if end proc:

f3

Error, (in f) invalid x: -3

lastexception

f&comma;invalid x: %1&comma;3

(1)

StringTools:-FormatMessagelastexception2..1

invalid x: -3

(2)

tryf3&semi;printf%s &comma;no error occurredcatchinvalid x&colon;printf%s &comma;error occurredcatch&colon;errorend try

error occurred

lastexception

f&comma;invalid x: %1&comma;3

(3)

See Also

lasterror, Procedures, return, StringTools[FormatMessage], try, WARNING


Download Help Document

Was this information helpful?



Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam