@@ -35,8 +35,13 @@ mutable struct MSession
35
35
ptr:: Ptr{Cvoid}
36
36
buffer:: Vector{UInt8}
37
37
bufptr:: Ptr{UInt8}
38
+ check_exceptions:: Bool
38
39
39
- function MSession (bufsize:: Integer = default_output_buffer_size; flags= default_startflag)
40
+ function MSession (
41
+ bufsize:: Integer = default_output_buffer_size;
42
+ flags= default_startflag,
43
+ check_exceptions:: Bool = true ,
44
+ )
40
45
if Sys. iswindows ()
41
46
assign_persistent_msession ()
42
47
end
@@ -72,7 +77,7 @@ mutable struct MSession
72
77
bufptr = convert (Ptr{UInt8}, C_NULL )
73
78
end
74
79
75
- self = new (ep, buf, bufptr)
80
+ self = new (ep, buf, bufptr, check_exceptions )
76
81
finalizer (release, self)
77
82
return self
78
83
end
@@ -101,6 +106,13 @@ function close(session::MSession)
101
106
return nothing
102
107
end
103
108
109
+ has_exception_check_enabled (session:: MSession = get_default_msession ()) =
110
+ session. check_exceptions
111
+ disable_exception_check! (session:: MSession = get_default_msession ()) =
112
+ (session. check_exceptions = false ; nothing )
113
+ enable_exception_check! (session:: MSession = get_default_msession ()) =
114
+ (session. check_exceptions = true ; nothing )
115
+
104
116
# default session
105
117
106
118
const default_msession_ref = Ref {MSession} ()
153
165
#
154
166
# ##########################################################
155
167
156
- function eval_string (session:: MSession , stmt:: String )
168
+ function _eval_string (session:: MSession , stmt:: String )
157
169
# evaluate a MATLAB statement in a given MATLAB session
158
170
ret = ccall (eng_eval_string[], Cint, (Ptr{Cvoid}, Ptr{UInt8}), session, stmt)
159
171
ret != 0 && throw (MEngineError (" invalid engine session (err = $ret )" ))
@@ -168,6 +180,13 @@ function eval_string(session::MSession, stmt::String)
168
180
return nothing
169
181
end
170
182
183
+ function eval_string (session:: MSession , stmt:: String )
184
+ _eval_string (session, stmt)
185
+ if session. check_exceptions
186
+ check_and_clear_last_exception (session)
187
+ end
188
+ end
189
+
171
190
eval_string (stmt:: String ) = eval_string (get_default_msession (), stmt)
172
191
173
192
function put_variable (session:: MSession , name:: Symbol , v:: MxArray )
@@ -208,6 +227,33 @@ get_mvariable(name::Symbol) = get_mvariable(get_default_msession(), name)
208
227
get_variable (name:: Symbol ) = jvalue (get_mvariable (name))
209
228
get_variable (name:: Symbol , kind) = jvalue (get_mvariable (name), kind)
210
229
230
+ """
231
+ check_and_clear_last_exception(session::MSession)
232
+
233
+ Checks if an exception has been thrown in the MATLAB session by checking the `MException.last` variable.
234
+ If it is not empty, it throws a `MatlabException` with the message and identifier of the last exception.
235
+ In any case, it clears the `MException.last` variable.
236
+ """
237
+ function check_and_clear_last_exception (session:: MSession )
238
+ exception_check_code = """
239
+ matlab_exception_jl_message = MException.last.message;
240
+ matlab_exception_jl_identifier = MException.last.identifier;
241
+ MException.last('reset');
242
+ """
243
+ _eval_string (session, exception_check_code)
244
+ message = jvalue (get_mvariable (session, :matlab_exception_jl_message ))
245
+ identifier = jvalue (get_mvariable (session, :matlab_exception_jl_identifier ))
246
+
247
+ if ! isempty (identifier)
248
+ throw (MatlabException (identifier, message))
249
+ end
250
+
251
+ _eval_string (
252
+ session,
253
+ " clear matlab_exception_jl_message matlab_exception_jl_identifier;" ,
254
+ )
255
+ end
256
+
211
257
# ##########################################################
212
258
#
213
259
# macro to simplify syntax
0 commit comments