Logtalk user manual
Error handling
Contents > User Manual > Error handling

# Error handling

All error handling is done in Logtalk by using the ISO defined `catch/3` and `throw/1` predicates [ISO 95]. Errors thrown by Logtalk have the following format:

    error(Error, logtalk(Goal, Entity))

In this exception term, `Goal` is the goal that triggered the error `Error` and `Entity` is the entity in whose context `Goal` is called. For example:

    error(permission_error(modify, private_predicate, p(_)), logtalk(foo::abolish(p/1), user))

Note, however, that `Goal` and `Entity` can be variables when the corresponding information is not available (usually due to compiler optimizations that throw away the necessary error context information).

## Compiler warnings and errors

The current Logtalk compiler uses the `read_term/3` ISO Prolog defined built-in predicate to read and compile a Logtalk source file. One consequence of this is that invalid Prolog terms or syntax errors may abort the compilation process with limited information given to the user (due to the inherent limitations of the `read_term/3` predicate).

If all the terms in a source file are valid, then there is a set of errors or potential errors, described below, that the compiler will try to detect and report, depending on the used compiler flags (see the Writing, running, and debugging programs section of this manual for details).

### Unknown entities

The Logtalk compiler will warn us of any referenced entity that is not currently loaded. The warning may reveal a misspell entity name or just an entity that it will be loaded next.

### Singleton variables

Singleton variables in a clause are often misspell variables and, as such, one of the most common errors when programming in Prolog. If your Prolog compiler complies with the Prolog ISO standard or at least supports the ISO predicate `read_term/3` called with the option `singletons(S)`, then the Logtalk compiler will warn us of any singleton it finds while compiling a Logtalk entity.

### Redefinition of Prolog built-in predicates

The Logtalk compiler will warn us of any redefinition of a Prolog built-in predicate inside an object or category. Sometimes the redefinition is intended. In other cases, the user may not be aware that the subjacent Prolog compiler may already provide the predicate as a built-in or we may want to ensure code portability among several Prolog compilers with different sets of built-in predicates.

### Redefinition of Logtalk built-in predicates

Similar to the redefinition of Prolog built-in predicates, the Logtalk compiler will warn us if we try to redefine a Logtalk built-in. The redefinition will probably be an error in almost all (if not all) cases.

### Redefinition of Logtalk built-in methods

An error will be thrown if we attempt to redefine a Logtalk built-in method inside an entity. The default behavior is to report the error and abort the compilation of the offending entity.

### Misspell calls of local predicates

A warning will be reported if Logtalk finds (in the body of a predicate definition) a call to a local predicate that is not defined, built-in (either in Prolog or in Logtalk) or declared dynamic. In most cases these calls are simple misspell errors.

### Portability warnings

A warning will be reported if a predicate clause contains a call to a non-ISO specified built-in predicate or to a non-ISO specified arithmetic function.

### Other warnings and errors

The Logtalk compiler will throw an error if it finds a predicate clause or a directive that cannot be parsed. The default behavior is to report the error and abort the compilation of the offending entity.

## Runtime errors

This section briefly describes runtime errors that result from misuse of Logtalk built-in predicates, built-in methods or from message sending. For a complete and detailed description of runtime errors please consult the Reference Manual.

### Logtalk built-in predicates

Most Logtalk built-in predicates checks the type and mode of the calling arguments, throwing an exception in case of misuse.

### Logtalk built-in methods

Most Logtalk built-in method checks the type and mode of the calling arguments, throwing an exception in case of misuse.

### Message sending

The message sending mechanisms always check if the receiver of a message is a defined object and if the message corresponds to a declared predicate within the scope of the sender. The built-in protocol `forwarding` declares a predicate, `forward/1`, which is automatically called (if defined) by the runtime for any message that the receiving object does not understand. The usual definition for this error handler is to delegate or forward the message to another object that might be able to answer it:

    forward(Message) :-
        [Object::Message].	% forward the message while preserving the original sender

If preserving the original sender is not required, this definition can be simplified to:

    forward(Message) :-
        Object::Message.

More sophisticated definitions are, of course, possible.