traperror
-- trap errorstraperror
(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(object)
traperror(object, t)
object |
- | any MuPAD object |
t |
- | the time limit: a nonnegative integer |
a nonnegative integer.
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
'). The error code is 1028
if the error was
raised by the command error
.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 4.traperror
:
if traperror((x := SomeErrorProneFunction())) <> 0 then DoSomethingWith(x); else RespondToTheError(); end_if;
lasterror
to
reproduce the trapped error.traperror
is a function of the system kernel.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))
1025
>> lasterror()
Error: Division by zero [_power]
The following attempt to compute a huge floating point number fails because of numerical overflow. The corresponding error code is 20:
>> traperror(exp(12345678.9))
20
>> lasterror()
Error: Overflow/underflow in arithmetical operation; during evaluation of 'exp::float'
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!"))
1028
>> lasterror()
Error: My error!
We try to factor a polynomial, but give up after ten seconds:
>> traperror(factor(x^1000 + 4*x + 1), 10)
1320
>> lasterror()
Error: Execution time exceeded; during evaluation of 'faclib::univ_mod_gcd'
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)
1320
>> lasterror()
Error: Execution time exceeded