I have a program, written in C++, which uses WSTP to communicate with Mathematica 10 (i.e., linked using Install["name_program"]
).
How should my code report an internal error? I've read through the WRI docs on Error and Interrupt Handling, but unfortunately they only talk about an instance where there are problems with the connection between the program and Mathematica, which doesn't apply to my case.
Example:
extern "C" int get_number(int param)
{
try
{
//calculate result
return result;
}
catch(...)
{
//What should be here?
}
}
Try
/Catch
block is not useful, unless you are throwing your own errors. So, you put whatever you want. More specifically, each put/get function returns abool
(actually anint
, but 0 isFalse
, anything else isTrue
) indicating if it has succeeded, and if it returns false, you handle it then. Your handling it could beThrow
to a common error handler, but whether or not that is useful is for you to decide. $\endgroup$Manual
return type in MathLink and return values explicitly. Then you'll have the option of returning e.g.$Failed
(a symbol) or a number, or whatever expression you like. Generally: don't try to do too much in C, it's too much work. Make the C side as simple as you can and have a Mathematica side wrapper to handle more complex situations such as error reporting. $\endgroup$