| Writing S-Functions | ![]() |
Handling Errors
When working with S-functions, it is important to handle unexpected events correctly such as invalid parameter values.
If your S-function has parameters whose contents you need to validate, use the following technique to report errors encountered.
ssSetErrorStatus(S,"error encountered due to ..."); return;
Note that the second argument to ssSetErrorStatus must be persistent memory. It cannot be a local variable in your procedure. For example, the following will cause unpredictable errors.
mdlOutputs()
{
char msg[256]; {ILLEGAL: to fix use "static char
msg[256];"}
sprintf(msg,"Error due to %s", string);
ssSetErrorStatus(S,msg);
return;
}
The ssSetErrorStatus error handling approach is the suggested alternative to using the mexErrMsgTxt function. The function mexErrMsgTxt uses exception handling to immediately terminate S-function execution and return control to Simulink. In order to support exception handling inside of S-functions, Simulink must set up exception handlers prior to each S-function invocation. This introduces overhead into simulation.
| Function-Call Subsystems | Exception Free Code | ![]() |