Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

connect signal to lambda with ability to trow exception inside lambda



  • Hi community,
    I have the need of connecting a signal to a lambda function;
    as soon some requirements pass, the lambda function throughs an exception, which is catch outside the connect.

    here simple part of the code:

    try 
          {
             connect(pHttpService, &CHttpService::dataReceived, [pHttpService, &pTcpSocketShutdown](QTcpSocket *pTcpSocket) throw(CUserAbortTestCaseException) {
                const QStringList data = pHttpService->requestData(pTcpSocket);
                if (data.first() == "GET" && data.contains("/shutdown"))
                {
                   DEBUG_LOG << "FORCE SHUTDOWN!" << data;
                   pTcpSocketShutdown = pTcpSocket;
                   throw new CUserAbortTestCaseException(E_ABORT_ALL);
                }
                else
                {
                   WARNING_LOG << "other data received!" << data;
                   pHttpService->sendResponse(pTcpSocket, "<h1>Response from ConfTest</h1>");
                }
             });
    
            ....
    
          } // try
          catch (CUserAbortTestCaseException *e)
          {
             // handle /shutdown request
             if (pTcpSocketShutdown != nullptr)
             {
                ...
                pHttpService->sendResponse(pTcpSocketShutdown, "<h1>Force Shutdown executed!</h1>");
                pTcpSocketShutdown = nullptr;
             }
          ...
       }
    

    Badly I got this compiler warning / error

    1>...myfile.cpp(567): error C2220: warning treated as error - no 'object' file generated
    1>...myfile.cpp(567): warning C4290: C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
    1>...myfile.cpp(580): warning C4290: C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
    

    Is there a way to handle exception throw from within lambda connected to signals?
    maybe the connect macro did not support throw() ?

    Thanks


  • Qt Champions 2019

    @Juergen_Skrotzky Why do you want to throw exceptions from slots? With queued connections you don't even know exactly when the slot will be executed, so you will not be able to catch the exception in these cases.



  • hi @jsulm I have a "http server" backend listen for some requests; and in this try block I do some stuff;
    as soon the request comes in, I need to leave this complete try block and clean up some parts.
    Maybe some heavy loops ar wait conditions are inside the try block - so it is important to jump out by exception.

    Otherwise I had to add some kind of polling for a flag on multiple party inside this try block...

    Do you have a better idea?

    Currently if I remove the define throw from the lambda, all throws are allowd - but than I got Unhandled exception

    So it seams that it is not possible to throw exception from within this lambda function. Is it right?


  • Moderators

    @Juergen_Skrotzky said in connect signal to lambda with ability to trow exception inside lambda:

    as soon the request comes in, I need to leave this complete try block and clean up some parts.

    what makes you think, that the signal will interrupt your current "try block" ?



  • @J-Hilk you are right - eventuloop is also busy in this case. I will store it and ask on a method if request arrived - istead of fire the exception.

    Thx guys for fast reply and hints!



  • @Juergen_Skrotzky said in connect signal to lambda with ability to trow exception inside lambda:

    Maybe some heavy loops ar wait conditions are inside the try block - so it is important to jump out by exception.

    Perhaps, but signals/slots mechanism is most of the time "asynchronous". It needs to got through the event loop so there is no chance that will match with your use case heavy loops inside try / catch block.

    I think you have to rethink your software structure.


Log in to reply