Main Content

Handle Python Exceptions

MATLAB® catches exceptions thrown by Python® and converts them into a matlab.exception.PyException object, which is derived from the MException class. For example:

try
  py.list('x','y',1)
catch e
  e.message
  if(isa(e,'matlab.exception.PyException'))
    e.ExceptionObject
  end
end
ans =

Python Error: TypeError: list() takes at most 1 argument (3 given)


ans = 

  Python tuple with no properties.

    (<type 'exceptions.TypeError'>, TypeError('list() takes at most 1 argument (3 given)',), None)

If MATLAB displays an error message of the following format, refer to your Python documentation for more information.

Python Error: Python class: message

See Also