• Places
    • Home
    • Graphs
    • Prefixes
  • Admin
    • Users
    • Settings
    • Plugins
    • Statistics
  • CPACK
    • Home
    • List packs
    • Submit pack
  • Repository
    • Load local file
    • Load from HTTP
    • Load from library
    • Remove triples
    • Clear repository
  • Query
    • YASGUI SPARQL Editor
    • Simple Form
    • SWISH Prolog shell
  • Help
    • Documentation
    • Tutorial
    • Roadmap
    • HTTP Services
  • Login

1 A C++ interface to SWI-Prolog
All Application Manual Name SummaryHelp

  • Documentation
    • Reference manual
    • Packages
      • A C++ interface to SWI-Prolog
        • A C++ interface to SWI-Prolog
          • Summary of changes between Versions 1 and 2
          • A simple example
          • Sample code
          • Introduction
          • The life of a PREDICATE
          • Overview
          • Examples
          • Rationale for changes from version 1
          • Porting from version 1 to version 2
          • The class PlFail
          • Overview of accessing and changing values
          • The class PlRegister
          • The class PlQuery
          • The PREDICATE and PREDICATE_NONDET macros
          • Exceptions
            • The class PlException
          • Embedded applications
          • Considerations
          • Conclusions

1.15 Exceptions

See also Prolog exceptions in foreign code.

Prolog exceptions are mapped to C++ exceptions using the class PlException (a subclass of PlExceptionBase 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, convenience functions have been created to exploit both their constructors for easy creation of these exceptions. If you wish to trap these, you should use PlException or PlExceptionBase and then look for the appropriate error name. For example, the following code catches "type_error" and passes all other exceptions:

try
{ do_something(...);
} catch (const PlException& e)
{ PlTerm e_t = e.term();
  PlAtom ATOM_type_error("type_error");
  // e_t.name() == PlAtom("error") && e_t.arity() == 2
  if ( e_t[1].name() == ATOM_type_error) )
  { ... // expected type and culprit are \exam{e_t[1][1]} and \exam{e_t[1][2]}
  } else throw;
}

The convenience functions are PlTypeEror() and PlDomainError(), PlDomainError(), PlInstantiationError(), PlExistenceError(), PlUninstantiationError(), PlRepresentationError(), PlPermissionError(), PlResourceError(), PlUnknownError(). There is also a PlGeneralError(inside) that creates error(inside,_) terms and is used by the other error convience functions.

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 1.17.2.

  char *data = "users";

  throw PlException(PlCompound("no_database", PlTerm(data)));

1.15.1 The class PlException

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

PlException :: PlException(const PlTerm &)
Create an exception from a general Prolog term. This provides the interface for throwing any Prolog terms as an exception.
std::string as_string()
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 << ex.as_string() << endl;
  }
int plThrow()
Used in the PREDICATE() wrapper to pass the exception to Prolog. See PL_raise_exeption().

1.15.1.1 The function PlTypeError

A type error expresses that a term does not satisfy the expected basic Prolog type.

PlTypeError :: PlTypeError(const std::string& expected, const PlTerm &actual)
Creates an ISO standard Prolog error term expressing the expected type and actual term that does not satisfy this type.

1.15.1.2 The function PlDomainError

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.

PlDomainError :: PlDomainError(const std::string& expected, const PlTerm &actual)
Creates an ISO standard Prolog error term expressing a the expected domain and the actual term found.

ClioPatria (version V3.1.1-51-ga0b30a5)