How to throw an exception when using MATLAB C or C++ API
8 次查看(过去 30 天)
显示 更早的评论
I have a C++ code, which is like:
mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
try{
// Here is my code
}catch(...){
mexErrMsgTxt("Something Wrong");
}
}
After mex the code, I want to get "Something Wrong" in MTALAB window if any exception was throwed from the "try" part.
However, if there do some errors in the code (inside the try curly bracket), it will crush my MATLAB like
The try-catch seems ineffective in this stuation. So how to throw an exception from C++ to MATLAB if anything wrong?
0 个评论
采纳的回答
Aiswarya
2023-10-3
Hi,
I understand that you are trying to throw an exception from C++ to MATLAB after converting the code to a MEX function.
The try-catch method results in a MATLAB crash. However, you can use the mexErrMsgIdAndTxt function : https://www.mathworks.com/help/matlab/apiref/mexerrmsgidandtxt.html?searchHighlight=mexErrMsgIdAndTxt&s_tid=srchtitle_support_results_1_mexErrMsgIdAndTxt&s_tid=mwa_osa_a
This displays the error in the MATLAB command window.
You may refer to a C++ example code (mexcpp.cpp) which implements error handling by running the following command in the MATLAB Command Window:
edit([matlabroot '/extern/examples/mex/mexcpp.cpp']);
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!