[ Event Handling | The ECLiPSe Built-In Predicates | Reference Manual | Alphabetic Index ]

error(+EventId, ?Culprit, +Module)

An error EventId is raised with Culprit (in module Module) the culprit, and the corresponding error handler is executed.
+EventId
Atom, integer or structure with functor default/1.
?Culprit
Prolog term.
+Module
Atom.

Description

The event or error EventId with Culprit as its culprit goal is raised. EventId is either an integer error number, or an event name atom. The error handler which is associated with EventId is invoked, with its first argument set to EventId, its second argument to Culprit, and its third argument to Module. If the handler has less than three arguments, the extra information is lost.

This simulates an occurrence of the error EventId inside a call to Culprit in Module. The valid error numbers are those returned by current_error/1. Event names can be any atom as long as an event handler has been defined for them.

If EventId is a structure with functor default/1, the structure argument is taken as the error number and the default handler is executed, even if the error handler has been redefined using set_event_handler/2. This is useful for writing user error handlers.

If the event handler succeeds, possibly binding some variables inside Culprit, then error/2 succeeds as well. If the handler fails or calls exit_block/1, then so does error/3.

Not that when the error handler ignores the module (ie. has arity less than three), then error/2 and error/3 are equivalent.

Fail Conditions

Fails if the handler fails.

Resatisfiable

No.

Exceptions

(4) instantiation fault
EventId is not instantiated.
(5) type error
EventId is not an error specification.
(6) out of range
EventId is a number but not a valid error number.
(32) no handler for event
No handler is associated to EventId

Examples

Success:

    [eclipse]: error(68, length(X, Y), lists).
    calling an undefined procedure length(_g56, _g58) in module lists

   % writing an alternative error handler for undefined predicates:
    [eclipse]: arg(1,2).           % we want to change this
    calling an undefined procedure arg(1, 2) in module eclipse

    [eclipse]: [user].             % compile the new handler

     undef_handler(_, Goal, Module) :-
            functor(Goal, Name, BadArity),
            (
                current_predicate(Name/Arity)@Module
            ;
                current_built_in(Name/Arity)@Module
            ),
            !,
            printf("%w does not exist, but there is %w\n",
                               [Name/BadArity, Name/Arity]),
            fail.
     undef_handler(Err, Goal, Module) :-
            error(default(Err), Goal, Module).
     user compiled 592 bytes in 0.02 seconds

    yes.
    [eclipse]: set_event_handler(68, undef_handler/3).

    yes.
    [eclipse]: arg(1,2).            % check if it works
    arg / 2 does not exist, but there is arg / 3

    no.

Error:
      error(N,dummy(1),eclipse).    (Error 4).
      error(5.0,dummy(1),eclipse).  (Error 5).
      error(-2,dummy(1),eclipse).   (Error 6).
      error(95,dummy(1),eclipse).   (Error 6).



See Also

error / 2, event / 1, current_error / 1, error_id / 2, get_event_handler / 3, reset_event_handler / 1, reset_error_handlers / 0, set_event_handler / 2