Error and Interrupt Handling

When you are putting and getting data via the Wolfram Symbolic Transfer Protocol (WSTP) various kinds of errors can occur. Whenever any error occurs, WSTP goes into a completely inactive state, and all WSTP functions you call will return 0 immediately.

Handling errors in WSTP programs.

When you do complicated operations, it is often convenient to check for errors only at the end. If you find that an error occurred, you must then call (NonWLLink[WSClearError(), paclet:ref/c/WSClearError]) to activate WSTP again.

Clearing out the remains of a packet.

After an error, it is common to want to discard the remainder of the packet or expression that you are currently processing. You can do this using (NonWLLink[WSNewPacket(), paclet:ref/c/WSNewPacket]).

In some cases, you may want to set it up so that if an error occurs while you are processing particular data, you can then later go back and reprocess the data in a different way. You can do this by calling (NonWLLink[WSCreateMark(), paclet:ref/c/WSCreateMark]) to create a mark before you first process the data, and then calling (NonWLLink[WSSeekMark(), paclet:ref/c/WSSeekMark]) to seek back to the mark if you need to reprocess the data. You should not forgot to call (NonWLLink[WSDestroyMark(), paclet:ref/c/WSDestroyMark]) when you have finally finished with the data—otherwise WSTP will continue to store it.

Aborting an external program.

If you interrupt the Wolfram Language while it is in the middle of executing an external function, it will typically give you the opportunity to try to abort the external function. If you choose to do this, what will happen is that the global variable (NonWLLink[WSAbort, paclet:ref/c/WSAbort]) will be set to 1 inside your external program.

WSTP cannot automatically back out of an external function call that has been made. So if you have a function that can take a long time, you should explicitly check (NonWLLink[WSAbort, paclet:ref/c/WSAbort]) every so often, returning from the function if you find that the variable has been set.