2.13.13 Advanced Topic: Error and Interrupt Handling
When you are putting and getting data via MathLink various kinds of errors can occur. Whenever any error occurs, MathLink goes into a completely inactive state, and all MathLink functions you call will return 0 immediately.
Handling errors in MathLink 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 MLClearError() to activate MathLink 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 MLNewPacket().
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 MLCreateMark() to create a mark before you first process the data, and then calling MLSeekMark() to seek back to the mark if you need to reprocess the data. You should not forgot to call MLDestroyMark() when you have finally finished with the data—otherwise MathLink will continue to store it.
Aborting an external program.
If you interrupt Mathematica 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 MLAbort will be set to 1 inside your external program.
MathLink 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 MLAbort every so often, returning from the function if you find that the variable has been set.