Error Handling

Syntactically incorrect queries are ignored by the Prolog query answer loop. The end-user might issue queries that are not syntactically correct. Here is an example:

?- X = .

The interpreter detects syntax errors and reports them to the end-user. The query text except for the initial comments is repeated and the error position is indicated. Also the syntax message is displayed:

Error: Term missing.
X =
  ^
?-

Queries with syntax errors are not executed and the interpreter returns to its prompt. It can further happen that a query causes an unhandled execution error during its execution. The Prolog query answer loop aborts the query upon unhandled execution errors. Here is an example:

?- foobar.

When an unhandled execution error happens, the interpreter will stop searching and report it to the end-user. The execution error message and its context will be display. For affected goal originating in the query the contexts will simply consist of the predicate identification:

Error: Undefined predicate foobar / 0.
     foobar / 0
?-

When the affected goal originates from a source file a more elaborated context is displayed. In particular the context will also show the file name of the absolute source path together with a line number where the affected goal can be found. The context will also show the stack trace from the affected goal back to the top level:

?- t1.
Error: Undefined predicate foobar / 0.
foobar / 0 in 'exception.p' at 5
t3 / 0 in 'exception.p' at 3
t2 / 0 in 'exception.p' at 1
t1 / 0
?-

The stack frame elimination optimization will remove stack frames from the call chain. As a result the stack trace will be shortened. In the extreme only the affected goal together with the query answer loop goal will remain in the call chain:

?- s1.
Error: Undefined predicate foobar / 0.
foobar / 0 in 'exception.p' at 12
s1 / 0
?-

The Prolog text can handle execution errors via the constructs catch/3 and finally/2. When an execution error is fully handled, that is when the execution error is not re-thrown and no other execution error occurs, then the Prolog query answer loop will not display any execution error and the query execution will continue as defined by the handler. The Prolog text cannot handle system errors, since the construct catch/3 is not able to handle them.

System errors have a special handling. When a system error of type user abort percolates to the query answer loop the interpreter will return to the Prolog prompt (?-). In case of a system error of type user exit the interpreter will leave this query answer loop. For all other system errors the interpreter will leave all query answer loops. In all cases the display of the execution error message or its context is suppressed.

Comments