[PyQt] Raising C++ exceptions from python

Alessandro Pasotti apasotti at gmail.com
Mon Oct 13 13:31:03 BST 2014


Thanks!

It works perfectly now.

2014-10-13 13:51 GMT+02:00 Phil Thompson <phil at riverbankcomputing.com>:
> On 13/10/2014 11:48 am, Alessandro Pasotti wrote:
>>
>> 2014-10-13 10:49 GMT+02:00 Phil Thompson <phil at riverbankcomputing.com>:
>>>
>>> On 13/10/2014 8:48 am, Alessandro Pasotti wrote:
>>>>
>>>>
>>>> Hello,
>>>>
>>>> What is the recommended way to raise/throw a C++ exception from python
>>>> to be catched in C++ code?
>>>>
>>>> For example, I have this SIP code:
>>>>
>>>> class QgsMapServiceException
>>>> {
>>>> %TypeHeaderCode
>>>> #include <qgsmapserviceexception.h>
>>>> %End
>>>>   public:
>>>>     QgsMapServiceException( const QString& code, const QString& message
>>>> );
>>>>     QString code() const;
>>>>     QString message() const;
>>>> };
>>>>
>>>>
>>>> In python code I would like to raise this exception and catch it from
>>>> C++ FCGI loop.
>>>>
>>>> The problem is that Python doesn't know that QgsMapServiceException is
>>>> an exception:
>>>>
>>>>     raise QgsMapServiceException('ExceptionFilter', 'Test exception
>>>> raised from ExceptionFilter')
>>>> TypeError: exceptions must be old-style classes or derived from
>>>> BaseException, not QgsMapServiceException
>>>
>>>
>>>
>>>
>>> http://pyqt.sourceforge.net/Docs/sip4/directives.html#directive-%Exception
>>>
>>> Phil
>>
>>
>> Hi Phil,
>>
>> Thanks for pointing me to the right docs, I've now this SIP code but I
>> cannot catch anything from C++ loop, it seems like the exceptions are
>> silently ignored. I would like to raise exceptions from python and
>> catch them in C++.
>>
>> Any idea about where to look?
>> Should I throw the exception from within RaiseCode ? (tried without
>> success).
>>
>>
>>
>> %Exception QgsMapServiceException(SIP_Exception)
>> /PyName=QgsMapServiceException/
>> {
>> %TypeHeaderCode
>> #include <qgsmapserviceexception.h>
>> %End
>> %RaiseCode
>>   SIP_BLOCK_THREADS
>>   PyErr_SetString(sipException_QgsMapServiceException,
>> sipExceptionRef.message().toUtf8().constData() );
>>   SIP_UNBLOCK_THREADS
>> %End
>> };
>
>
> You might need...
>
> http://pyqt.sourceforge.net/Docs/sip4/directives.html#directive-%VirtualErrorHandler
>
> Phil



-- 
Alessandro Pasotti
w3:   www.itopen.it


More information about the PyQt mailing list