traperror (and variable lasterror) - Maple Programming Help

Home : Support : Online Help : System : Libraries and Packages : Deprecated Packages and Commands : Deprecated commands : traperror(deprecated)

traperror (and variable lasterror)

trap an error condition

 Calling Sequence traperror(expr1, expr2, ...)

Parameters

 expr1, expr2, ... - any expressions

Description

 • Important: The command traperror and the variable lasterror have been deprecated. Use the superseding construct try...catch...end try to trap errors instead.  The variable lastexception supercedes lasterror.
 • The traperror command sets an error trap prior to evaluating its arguments.  If an error occurs either during evaluation or during simplification, traperror returns the name, string, or expression sequence corresponding to the error message of the first error occurring.  Each call to traperror clears the $\mathrm{lasterror}$ variable before executing.
 • Otherwise, it simply returns its simplified evaluated arguments.
 • The most recently occurring error is stored in the variable lasterror. This variable can be manipulated as any other variable. The lasterror variable is useful for checking the results of traperror as in the following.

$\mathrm{if}\mathrm{traperror}\left(\mathrm{...}\right)=\mathrm{lasterror}\mathrm{then}\mathrm{...}$

 Note: The traperror command and the lasterror variable must appear in this order.
 • The following errors cannot be trapped, because they are not something from which a Maple program can reasonably recover.

 interrupted assertion failed stack overflow object too large out of memory

 • The time expired error, which is generated when the time limit set by the timelimit function expires, can be trapped only outside the call to timelimit.
 The time expired error is not caught by traperror within a computation being carried out under a time limit. This allows you to time-limit a computation that uses traperror.

 • The traperror (and variable lasterror) command is thread-safe as of Maple 15.

Examples

Important: The command traperror and the variable lasterror have been deprecated. Use the superseding construct try...catch...end try to trap errors instead.

 > $f≔u↦\frac{{u}^{2}-1}{u-1}$
 ${f}{≔}{u}{↦}\frac{{{u}}^{{2}}{-}{1}}{{u}{-}{1}}$ (1)
 > $\mathrm{printlevel}≔3:$
 > $\mathbf{for}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}x\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{in}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\left[0,1,2\right]\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}r≔\mathrm{traperror}\left(f\left(x\right)\right);\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathbf{if}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}r=\mathrm{lasterror}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{then}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathbf{if}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}r=\mathrm{division by zero}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{then}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}r≔\mathrm{limit}\left(f\left(u\right),u=x\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathbf{else}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathbf{error}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\left(\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{if}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{if};\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathrm{lprint}\left(\mathrm{Result: x =},x,\mathrm{f\left(x\right) =},r\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}$
 Result:  x =, 0, f(x) =, 1