[PyQt] Raising C++ exceptions from python

Phil Thompson phil at riverbankcomputing.com
Mon Oct 13 09:49:43 BST 2014


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


More information about the PyQt mailing list