[Qt5-feedback] Qt5 and C++ exception

Arvid Ephraim Picciani arvid.picciani at nokia.com
Mon Jun 27 10:25:20 CEST 2011


On 06/27/2011 08:26 AM, ext Дмитрий Б. wrote:
> Sorry guys if this question has already been discussed. Is any plans to
> make changes in Qt-API to be able to use C++ exceptions, as in the other
> frameworks?
> Like this:
>
> QThread* thread = new QThread();
> try
> {
> thread->start();
> }
> catch (e)
> {
> //Error, thread has not been created
> }


While this is a simple case (if you ignore abi quirks), consider this:

class MyWidget: public QWidget {
private:
     void startStuff();
     {
	thread->start();
     }
protected:
    virtual void	keyPressEvent ( QKeyEvent * event )
    {
	// if bla
         startStuff();
    }
}



What if start() throws now?
Where do you expect your "catch" ?
Either way, you can't unwind the eventloop. For once because that could 
contain native calls that contain no unwind information.

To get reliable code, you will end up with this:

     void startStuff();
     {
         try {
               thread->start();
         } catch(e) {
               setError(e);
         }
     }


Which is exactly what Qt does in the first place, without the boilerplate.


More information about the Qt5-feedback mailing list