I know that there is no built-in way to do try-catch statements in C, but has MATLAB facilitated any type of try-catch functionality for MEX files? I tried to use Longjmp
and Setjmp
in the way described in http://www.di.unipi.it/~nids/docs/longjump_try_trow_catch.html, but I was not successful. Has anyone else figured out a way?
Asked
Active
Viewed 844 times
4

Florian Brucker
- 9,621
- 3
- 48
- 81

robguinness
- 16,266
- 14
- 55
- 65
-
Been a while, but I seem to remember trying to run a mex dll through visual studio and then specifying matlab.exe as the program to start. Alternatively, you could attach to matlab.exe from VS and load up your compiled mex debug symbols. – Meirion Hughes Jun 19 '13 at 11:57
-
1Where does the "error" come from? C has no concept of exceptions, so what is it you want to catch, exactly? – wakjah Jun 19 '13 at 14:16
-
@robguinness: Your URL has a typo: [http://www.di.unipi.it/~nids/docs/longjump_try_trow_catch.html](http://www.di.unipi.it/~nids/docs/longjump_try_trow_catch.html). Also, did you figure out why the `setjmp.h` method doesn't work for you? – horchler Jun 19 '13 at 14:42
-
@horchler: On SO you are free to fix these kinds of errors yourself (just click the edit link below the post). I've fixed the link in the post. – Florian Brucker Jun 19 '13 at 15:01
-
@FlorianBrucker: I realize that and you might be able to fix it with your higher reputation, but I cannot. I can only fix things if the edit consists of a minimum number of characters (10 in this case). It's annoying as a single character can make a huge difference and I'm an editor by nature. What else am I to do, put bogus characters in someone else's post to try to game the system? – horchler Jun 19 '13 at 16:28
-
@horchler: The minimum character limit is in place up to 2k reputation. For small but important edits one usually just adds a word (I capitalized MATLAB and MEX, for example). It's not about gaming the system, it's about improving the quality of the content :) See also [this thread on meta](http://meta.stackexchange.com/a/81522/214749). – Florian Brucker Jun 20 '13 at 06:02
1 Answers
3
You can write your MEX-files in C++ and use the C++ exception handling.

Florian Brucker
- 9,621
- 3
- 48
- 81
-
@robguinness: I know. However, in many settings it's possible for people to switch to C++ without much hassle, so I think the information is valuable in this context. Of course you are free not to accept my answer :) I'm not aware of any MEX-specific pure C error handling mechanisms. – Florian Brucker Jun 19 '13 at 13:01
-
This is true. I guess if there's no way to do it in C, I'll probably use your suggestion. Waiting to see if there are any other answers though... – robguinness Jun 19 '13 at 14:03