Trap errors
MuPAD® notebooks are not recommended. Use MATLAB® live scripts instead.
MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.
traperror(object
) traperror(object
,t
) traperror(object
,MaxSteps = s
)
traperror(object)
traps errors produced by
the evaluation of object
.
traperror(object, t)
does the same. Moreover,
it stops the evaluation if it is not finished after a real time of t
seconds.
traperror
traps errors caused by the evaluation
of the object. Syntactical errors, i.e., errors on parsing the object,
cannot be caught. The same holds true for fatal errors causing the
termination of MuPAD^{®}.
traperror
returns the error code 0
if
no error happened. The error code is 1320
if the
given time limit t
is exceeded ("Execution
time exceeded
") and 1321
if the
given number of "execution steps" is exceeded. The error
code is 1028
if the error was raised by the command error
.
If traperror
is called with a numerical second
argument, this number is taken as a time limit, measured in seconds,
of "process time" (see the documentation of the time
function for a
discussion of this term).
When using the option MaxSteps = s
, the caller
sets a time limit which is not systemdependent, but rather measured
in terms of MuPAD evaluation steps.
The number s
does not refer
directly to evaluation steps, but rather to a fixed (large) number
of steps which may change from one MuPAD release to the next,
but is fixed within one release. The number s
is
twice the number of outputs caused by Pref::report(9)
for
a calculation using the maximum time allowed.
If traperror
has no time limit set and an
"Execution time exceeded
" error is
raised by an enclosing traperror(..., t)
command,
then this error is not trapped by the inner traperror
.
It is trapped by the traperror
call that has set
the time limit. Cf. Example 5.
The object can be an assignment which, for syntactical reasons,
must be enclosed in additional brackets. The following code fragment
demonstrates a typical application of traperror
:
if traperror((x := SomeErrorProneFunction())) = 0 then DoSomethingWith(x); else RespondToTheError(); end_if;
Use lasterror
to
reproduce the trapped error.
Errors that happen during the execution of kernel functions
have various error codes, depending on the problem. E.g., "Division
by zero
" produces the error code 1025:
y := 1/x: traperror(subs(y, x = 0))
lasterror()
Error: Division by zero. [_power]
All errors raised using the function error
have the error code 1028
.
Errors during the execution of library functions are of this kind:
traperror(error("My error!"))
lasterror()
Error: My error!
We try to factor a polynomial, but give up after ten seconds:
traperror(factor(x^1000 + 4*x + 1), 10)
lasterror()
Error: Execution time exceeded; Evaluating: faclib::univ_mod_gcd
For use inside other routines, it is preferable to use MaxSteps
instead
of a time limit, to achieve consistent results across slower and faster
machines:
traperror(factor(x^1000 + 4*x + 1), MaxSteps=10)
lasterror()
Error: Execution MaxSteps exceeded [traperror]; Evaluating: faclib::ddf
Note that evaluation steps may take vastly different amounts
of time, so even on the same machine, different expressions evaluated
with the same value of MaxSteps
may be terminated
after very different lengths of time:
time(traperror(factor(x^1000 + 4*x + 1), MaxSteps=1)); time(traperror(while TRUE do 1 end_while, MaxSteps=1)); time(traperror(int(1/sqrt(1/r1/r0), r=0..r0), MaxSteps=1))
Here we have two nested traperror
calls.
The inner call contains an unterminated loop and the outer call has
a time limit of 2
seconds. When the execution time
is exceeded, this special error is not trapped by the inner traperror
call.
Because of the error, print(1)
is never executed:
traperror((traperror((while TRUE do 1 end)); print(1)), 2)
lasterror()
Error: Execution time is exceeded.

Any MuPAD object 

The time limit: a positive integer 

The execution limit: a positive integer 
Nonnegative integer.