How do I return an error to C++ from a compiled Matlab dll?
3 次查看(过去 30 天)
显示 更早的评论
I have a Matlab function: foo=doFoo(); I have used the Matlab compiler to convert doFoo() to a function callable from C++: success=mlfDoFoo(int argOut, mxArray **foo); Every once in a while, doFoo crashes.
Is there a way to set success based on whether or not and how doFoo crashes?
0 个评论
回答(1 个)
Rahul Goel
2015-9-28
Fred,
The syntax to use shared libraries in C++ code is such that their return type is "void" and hence cannot be used in the way you mentioned. Also, what exactly do you mean by "crash"? Did the whole program crashed or is it just the library which throws an error? All the outputs to be collected are passed as reference using the second argument("mxArray **foo" in this case) of the function called from shared library, if it crashes there will not be any variable which might store this error information.
I would suggest trying the try/catch in your code while using the shared library. An example can be found in the documentation at:
where a try/catch block is used to handle any exception thrown by the function called from the shared library:
try
{
// Create input data
double data[] = {1,2,3,4,5,6,7,8,9};
mwArray in1(3, 3, mxDOUBLE_CLASS, mxREAL);
mwArray in2(3, 3, mxDOUBLE_CLASS, mxREAL);
in1.SetData(data, 9);
in2.SetData(data, 9);
// Create output array
mwArray out;
// Call the library function
addmatrix(1, out, in1, in2);
std::cout << "The value of added matrix is:" << std::endl;
std::cout << out << std::endl;
}
catch (const mwException& e)
{
std::cerr << e.what() << std::endl;
return -2;
}
catch (...)
{
std::cerr << "Unexpected error thrown" << std::endl;
return -3;
}
Hope this helps.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!