## `error` -- raise a user-specified exception

#### Introduction

`error(`message`)` aborts the current procedure, returns to the interactive level, and displays the error message `message`.

#### Call(s)

`error(message)`

#### Parameters

`message` | - | the error message: a string

#### Side Effects

The formatting of the output of `error` is sensitive to the environment variable `TEXTWIDTH`.

#### Related Functions

`lasterror`, `prog::error`, `traperror`, `warning`

#### Details

- The call `error(`message`)` aborts the current procedure with an error. If the error is not caught via `traperror` by a procedure that has directly or indirectly called the current procedure, control is returned to the interactive level, and the string `message` is printed as an error message.
- The printed error message has the form `Error: message [name]`, where `name` is the name of the procedure containing the call to `error`. See the examples.
- Errors can be caught by the function `traperror`. If an error occurs while the arguments of `traperror` are evaluated, control is returned to the procedure containing the call to `traperror` and not to the interactive level. No error message is printed. The return value of `traperror` is `1028` when it catches an error raised by `error`; see example 2.
- The function `error` is useful to raise an error in the type checking part of a user-defined procedure, when this procedure is called with invalid arguments.
- `error` is a function of the system kernel.

#### Example 1

If the divisor of the following simple division routine is `0`, then an error is raised:

    >> mydivide := proc(n, d) begin
         if iszero(d) then
           error("Division by 0")
         end_if;
         n/d
       end_proc:
       mydivide(2, 0)

          Error: Division by 0 [mydivide]

#### Example 2

When the error is raised in the following procedure `p`, control is returned to the interactive level immediately. The second call to `print` is never executed. Note that the procedure's name is printed in the error message:

    >> p := proc() begin
         print("entering procedure p");
         error("oops");
         print("leaving procedure p")
       end_proc:
       p()

                              "entering procedure p"
          Error: oops [p]

The following procedure `q` calls the procedure `p` and catches any error that is raised within `p`:

    >> q := proc() begin
         print("entering procedure q");
         print("caught error: ", traperror(p()));
         print("leaving procedure q")
       end_proc:
       q()

                              "entering procedure q"

                              "entering procedure p"

                              "caught error: ", 1028

                               "leaving procedure q"

#### Changes

- no changes

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