Did you know ... | Search Documentation: |
![]() | Exceptions (version 2) |
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
,
PlTermvDomainError
, PlInstantiationError
,
PlExistenceError
, PermissionError
, PlResourceError
,
and PlException_qid
.
To throw an exception, create an instance of PlException
and use throw
. This is intercepted by the PREDICATE macro
and turned into a Prolog exception. See section
2.19.2.
char *data = "users"; throw PlException(PlCompound("no_database", PlTerm(data)));
This subclass of PlTerm
is used to represent exceptions.
Currently defined methods are:
...; try { PlCall("consult(load)"); } catch ( PlException& ex ) { cerr << ex.as_string() << endl; }
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.