In addition to functions for passing data between Prolog an C, XSB contains other functions that may be useful in Foreign C code. We mention a few here that pertain to throwing exceptions from C code (cf. Volume 1 Chapter 8: Exception Handling). These functions can be used by code that uses either the lower- or higher-level interface.
Example: The code fragment
Cell num;
:
xsb_domain_error(CTXTc "not_less_than_zero",num,"atom_length",2,2);
in atom_length/2 gives rise to the behavior
| ?- atom_length(abcde,-1).
++Error[XSB/Runtime/P]: [Domain (-1 not in domain not_less_than_zero)]
in arg 2 of predicate atom_length/2)
Example: The code fragment
Cell tid;
:
xsb_existence_error(CTXTc "thread",reg[2],"xsb_thread_join",1,1);
in thread_join/1 gives rise to a the behavior
| ?- thread_join(7).
++Error[XSB/Runtime/P]: [Existence (No thread 1 exists)]
in arg 1 of predicate thread_join/1)
if a thread with thread id 7 does not exist.
Example: The code fragment
xsb_instantiation_error(CTXTc "atom_length",2,1,NULL);
in atom_length/2 gives rise to a the behavior
| ?- atom_length(X,Y). ++Error[XSB/Runtime/P]: [Instantiation] in arg 1 of predicate atom_length/2
Example: The code fragment
xsb_permission_error(CTXTc "unlock mutex","mutex not held by thread",
xsb_thread_id,"mutex_unlock",2);
in mutex_unlock/1 gives rise to a the behavior
| ?- mutex_unlock(mymut). ++Error[XSB/Runtime/P]: [Permission (Operation) unlock mutex on mutex not held by thread: 0] in predicate mutex_unlock/1)if thread 0 does not own mutex mymut.
Example: The code fragment
xsb_resource_error(th,"system threads","thread_create",2);in thread_create/1 gives rise to a the behavior
| ?- thread_create(X). ++Error[XSB/Runtime/P]: [Resource (system threads))] in predicate thread_create/2)If the number of system threads has been exceeded.
Example: The code fragment
Cell num;
:
if (!isinteger(num)) xsb_type_error(CTXTc "integer",num,"atom_length",2,2);
in atom_length/2 gives rise to the behavior
| ?- atom_length(foo,a). ++Error[XSB/Runtime/P]: [Type (a in place of integer)] in arg 2 of predicate atom_length/2)