Node:Exception Handling in C, Previous:Calling Prolog Asynchronously, Up:Calling Prolog



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, it must use the SP_raise_exception() function below. If an exception term exists, SP_exception_term() retracts it and stores it as the value of an SP_term_ref which must exist prior to the call and returns nonzero. Otherwise, it returns zero:

int SP_exception_term(SP_term_ref t)

To raise an exception from a C function called from Prolog, just call SP_raise_exception(t) where t is the SP_term_ref whose value is the exception term. The glue code 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:

void SP_raise_exception(SP_term_ref t)