Next: , Previous: Calling Prolog Asynchronously, Up: Calling Prolog from C

6.5.4 Exception Handling in C

When an exception has been raised, the functions SP_query(), SP_query_cut_fail() and SP_next_solution() return SP_ERROR. To access the exception term (the argument of the call to raise_exception/1), which is asserted when the exception is raised, the function SP_exception_term() is used. As a side-effect, the exception term is retracted, so if your code wants to pass the exception term back to Prolog, use SP_raise_exception(). To raise an exception from a C function called from Prolog, just call SP_raise_exception(). Upon return, Prolog will detect that an exception has been raised, any value returned from the function will be ignored, and the exception will be passed back to Prolog. Please note: this should only be called right before returning to Prolog. To propagate failure to Prolog, call SP_fail(). Upon return, Prolog will backtrack. Please note: this should only be called right before returning to Prolog. Prolog error handling is mostly done by raising and catching exceptions. However, some faults are of a nature such that when they occur, the internal program state may be corrupted, and it is not safe to merely raise an exception. In runtime systems, the C macro SP_on_fault() provides an environment for handling faults.

The function SP_raise_fault() can be used to raise a fault with an encoded string explaining the reason.

Send feedback on this subject.