10 Exceptions
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
    • Packages
      • A C++ interface to SWI-Prolog
        • Exceptions
          • The class PlException
          • The class PlTypeError
          • The class PlDomainError

10.1 The class PlException

This subclass of PlTerm is used to represent exceptions. Currently defined methods are:

PlException :: PlException(const PlTerm &t)
Create an exception from a general Prolog term. This is 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;
  }
}