1.10.1 The class PlException
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:
- PlException :: PlException()
- Create an exception term using PL_exception(0). The method is_null() succeeds if there was simple failure (e.g., from unification failing) and not_null() succeeds if there was an exception.
- PlException :: PlException(const PlTerm &)
- Create an exception from a general Prolog term. This provides the interface for throwing any Prolog terms as an exception.
- PlException ::operator wchar_t *(void)
- PlException ::operator char *(void)
- The exception is translated into a message as produced by
print_message/2.
The character data is stored in a ring. Example:
...; try { PlCall("consult(load)"); } catch ( PlException &ex ) { cerr << (char *) ex << endl; }
- int plThrow()
- Used in the PREDICATE() wrapper to pass the exception to Prolog. See PL_raise_exeption().
- int cppThrow()
- Used by PlQuery::next_solution() to refine a generic PlException
representing a specific class of Prolog exceptions to the corresponding
C++ exception class and finally then executes throw(). Thus, if a
PlException represents the
term
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; } }