How can I pass a cpp exception into python

Asked by Stefan Stiene

Hi,
how can I pass a cpp exception to python. I want to be able to catch the exception on python side with a

try
    myWrapperfunction
except Exception:
    ...

Is this possible?

stefan

Question information

Language:
English Edit question
Status:
Solved
For:
PyBindGen Edit question
Assignee:
No assignee Edit question
Solved by:
Gustavo Carneiro
Solved:
Last query:
Last reply:
Revision history for this message
Best Gustavo Carneiro (gjc) said :
#1

There are some unit tests that do that (see tests/foomodulegen.py). Example catch your own custom exception:

    Error = mod.add_exception('Error')
    DomainError = mod.add_exception('DomainError', parent=Error)
    mod.add_function('my_inverse_func', 'double', [Parameter.new('double', 'x')],
                     throw=[DomainError])

Example to catch a std::exception:

    std_exception = mod.add_exception('exception', foreign_cpp_namespace='std', message_rvalue='%(EXC)s.what()')
    mod.add_function('my_inverse_func2', 'double', [Parameter.new('double', 'x')],
                     throw=[std_exception])
    ClassThatThrows.add_method('my_inverse_method2', 'double', [Parameter.new('double', 'x')],
                               throw=[std_exception])

Revision history for this message
Stefan Stiene (sstiene) said :
#2

Thanks Gustavo Carneiro, that solved my question.