Path: news.mathworks.com!not-for-mail
From: "Philip Borghesani" <philip_borghesani@mathworks.spam>
Newsgroups: comp.soft-sys.matlab
Subject: Re: how to catch native C exception from Matlab user-defined function?
Date: Tue, 3 Nov 2009 18:03:38 -0500
Organization: The MathWorks, Inc.
Lines: 20
Message-ID: <hcqcsa$ik6$1@fred.mathworks.com>
References: <hcqa18$m8c$1@fred.mathworks.com>
Reply-To: "Philip Borghesani" <philip_borghesani@mathworks.spam>
NNTP-Posting-Host: borghesanip.dhcp.mathworks.com
X-Trace: fred.mathworks.com 1257289418 19078 172.31.46.134 (3 Nov 2009 23:03:38 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 3 Nov 2009 23:03:38 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.5512
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579
Xref: news.mathworks.com comp.soft-sys.matlab:582203


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