bmsherman/haskell-matlab

Better error-handling from Engine

Opened this issue · 2 comments

Possibly related: #10

Currently, some errors, like those causes by improper (or buggy) use of clearVar, cause output to happen in stderr (I think). We could detect this in MATLAB with try-catch and return MATLAB incantation of Either. See here for a discussion on the issue of using try/catch, however.

So, other avenues of attack may need to be considered as well.

I think #15 may provide a solution; we could ship a function tryToEither.m:

function result =  tryToEither(fun, varargin)
result =  struct('left', struct('message', 'tryToEither: try nor catch succeeded'));
try
  funResult = fun(varargin{:});
  result = struct('right', statementString);
catch ex
  result = struct('left', ex);
end
end

Of course we may need a similar function shipped to create a function handle, haven't tested that yet (or seen any evidence of function handle support in the MATLAB Engine API).

Also, regarding Haskell-exceptions and errors, I'm guessing that foreign calls can't initiate an exception (at least not one that can be handled), unless we implement a wrapper as described above. One possible caveat might be: if the FFI call somehow called back into Haskell, and an exception was thrown in the Haskell code at that point.

Chessai says: i believe that the thread running the c code will just inherit whatever exception is thrown. you can catch them from haskell using the handlers in posix (which is not supported on windows). Minimalist segfaulting haskell program: https://gist.github.com/chessai/a9ae3d56b8f64ea6dce6065808fa3726