A Prolog procedure, when called, may succeed, fail, or produce an error. There are four basic macros provided as exit points from a C external predicate:
This causes the external procedure to terminate normally with success.Succeed;
This causes the external procedure to terminate normally with failure.Fail;
This causes the external procedure to succeed if expr is true (in the C definition of truth, i.e. nonzero), otherwise it fails.Succeed_If(expr);
This causes the external procedure to terminate abnormally, raising the error type ErrorId. When Succeed or Fail is called control returns to the Prolog program which called the external procedure, and execution continues normally. A call to Error() is made when for some reason an error has occurred in the execution of the external procedure. The argument to Error() is taken from a list of predefined errors. Upon a call to Error(), control passes to the appropriate error handler, and execution continues as described in the section on error handling. The list of predefined errors is in the file 'error.h'. The most common errors will be:Error(ErrorId);
If, for instance, the file to open does not exist, this will result in an error message likeif (open(path, flags) < 0) { Set_Errno Error(SYS_ERROR) }
where the text "No such file or directory" is obtained from the operating system.system interface error: No such file or directory in my_external(...)
CAUTION: If the external predicate calls another function which uses some of the return or error macros, the external predicate has to test its return code and if it is negative, it must return to Prolog with this value:
int p_my_external(val, tag) value val; type tag; { ... if ((err = aux_func(val)) < 0) { Error(err) } ... } int aux_func(val) value val; { if (condition(val)) { Succeed } else { Error(TYPE_ERROR) } }
After performing an arithmetic operation on floating point numbers, which might produce an exception, use the macro
which raises the corresponding event in the Prolog execution, if necessary.Check_Float_Exception()