Thread Subject: how to catch native C exception from Matlab user-defined function?

Subject: how to catch native C exception from Matlab user-defined function?

From: Ian Bell

Date: 3 Nov, 2009 22:15:04

Message: 1 of 3

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?

From: Philip Borghesani

Date: 3 Nov, 2009 23:03:38

Message: 2 of 3

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?

From: Ian Bell

Date: 3 Nov, 2009 23:22:03

Message: 3 of 3

Thanks Philip

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com