## `traperror` -- trap errors

#### Introduction

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

#### Call(s)

`traperror(object)`
`traperror(object, t)`

#### Parameters

`object` | - | any MuPAD object
`t` | - | the time limit: a nonnegative integer

#### Returns

a nonnegative integer.

#### Related Functions

`error`, `prog::error`, `lasterror`

#### Details

- `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`.
- 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 4.
- 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.
- `traperror` is a function of the system kernel.

#### Example 1

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'

#### Example 2

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!

#### Example 3

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'

#### Example 4

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

#### Changes

- no changes

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000