Did you know ... | Search Documentation: |
Exceptions |
Prolog exceptions are mapped to C++ exceptions using the subclass PlException of PlTerm to represent the Prolog exception term. All type-conversion functions of the interface raise Prolog-compliant exceptions, providing decent error-handling support at no extra work for the programmer.
For some commonly used exceptions, subclasses of PlException have been created to exploit both their constructors for easy creation of these exceptions as well as selective trapping in C++. Currently, these are PlTypeEror and PlDomainError.
To throw an exception, create an instance of PlException and use throw().
char *data = "users"; throw PlException(PlCompound("no_database", PlTerm(data)));
The C++ model of exceptions and the Prolog model of exceptions are
different. Wherever the underlying function returns a "fail" return
code, the C++ API does a further check for whether there's an exception
and, if so, does a C++ throw
of a PlException
object. You can use C++ try-catch to intercept this and examine the
This subclass of PlTerm is used to represent exceptions. Currently defined methods are:
...; try { PlCall("consult(load)"); } catch ( PlException &ex ) { cerr << (char *) ex << endl; }
error(type_error(Expected, Actual)
, Context)
PlException::cppThrow() throws a PlTypeEror exception. This ensures consistency in the exception-class whether the exception is generated by the C++-interface or returned by Prolog.
The following example illustrates this behaviour:
PREDICATE(call_atom, 1) { try { return PlCall((char *)A1); } catch ( PlTypeError &ex ) { cerr << "Type Error caugth in C++" << endl; cerr << "Message: \"" << (char *)ex << "\"" << endl; return FALSE; } }
A type error expresses that a term does not satisfy the expected basic Prolog type.
A domain error expresses that a term satisfies the basic
Prolog type expected, but is unacceptable to the restricted domain
expected by some operation. For example, the standard Prolog open/3
call expect an io_mode
(read, write, append, ...). If an
integer is provided, this is a type error, if an atom other
than one of the defined io-modes is provided it is a domain error.