Assume a user-defined Matlab function (e.g. MyFunction) executes 'calllib' to call a function in a native C DLL and that the C function throws an exception (e.g. std::exception). Can the user-defined Matlab function use a MException try/catch block to catch the native C exception? If not, what is the recommended way to catch an exception thrown from within a native C DLL?
Thanks,
Ian
Subject: how to catch native C exception from Matlab user-defined function?
There is no way to catch a C++ exception in MATLAB from a function called with calllib. Note that such a function is not a C
function it is a C++ function. The best solution is to code a wrapper dll in c++ to catch the exceptions and return error codes or
make calls to mexErrMsgIdAndTxt. The wrapper dll can be loadable with loadlibrary or it can be a MEX function.
Most dlls that allow exceptions to be thrown from their functions have strict compiler compatibility requirements because of the
different ways compilers treat exceptions so you may need to build the helper dll with the same compiler that was used to build the
dll you are calling.
Phil
"Ian Bell" <ibell@instepsystems.com> wrote in message news:hcqa18$m8c$1@fred.mathworks.com...
> Assume a user-defined Matlab function (e.g. MyFunction) executes 'calllib' to call a function in a native C DLL and that the C
> function throws an exception (e.g. std::exception). Can the user-defined Matlab function use a MException try/catch block to
> catch the native C exception? If not, what is the recommended way to catch an exception thrown from within a native C DLL?
>
> Thanks,
>
> Ian
Subject: how to catch native C exception from Matlab user-defined function?
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for
all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content.
Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available
via MATLAB Central.
Read the complete Terms prior to use.