Throwing MException subclass from MEX file
1 回表示 (過去 30 日間)
古いコメントを表示
My code has a particular algorithm which is coded up both as a m-file (slower) and a MEX file (faster). I would like to throw a particular MException subclass (which captures particular information about what went wrong) from both versions of the algorithm (and let the outside function that calls either of them handle it).
Now, this is easy enough to do with throw() from inside the M-file but I am not sure how to do it from inside a MEX file.
My guess was that mexEvalString of called with argument "throw(...);" should have worked but it just returns control to MEX file and "contains" exception within. mexEvalStringWithTrap gets me a pointer to MException object, which I could use to handle the exception within C++ but I don't want to do exception handling without MEX, but rather in an M-file that calls either C++ or Matlab version of the code.
Is there a way around this? I'm running 2014b.
0 件のコメント
回答 (1 件)
Adam
2014 年 12 月 9 日
You could use
mexErrMsgIdAndTxt( "MyProg:Id", "Somoething went wrong.");
in the mex.
Then in a wrapper function that calls the mex catch this in a try catch wrapped around the mex call and then rethrow your MException subclass using the information from this exception.
Personally I use
mexErrMsgTxt(...)
in my code and I can try-catch this, but I don't get an error id which is not ideal. The above version will give you the error id for finer control of the exception types in the C++ code.
2 件のコメント
Adam
2014 年 12 月 9 日
I'm not aware of a way of passing custom classes out of mex other than in struct form via the standard mex outputs, but I haven't done a huge amount in mex so there may be other methods.
参考
カテゴリ
Help Center および File Exchange で Software Development Tools についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!