Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. connect signal to lambda with ability to trow exception inside lambda
Forum Updated to NodeBB v4.3 + New Features

connect signal to lambda with ability to trow exception inside lambda

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 709 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Juergen_Skrotzky
    wrote on last edited by Juergen_Skrotzky
    #1

    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

    jsulmJ 1 Reply Last reply
    0
    • J Juergen_Skrotzky

      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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • J Offline
        J Offline
        Juergen_Skrotzky
        wrote on last edited by
        #3

        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?

        J.HilkJ KroMignonK 2 Replies Last reply
        0
        • J Juergen_Skrotzky

          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?

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @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" ?


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          J 1 Reply Last reply
          2
          • J.HilkJ J.Hilk

            @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 Offline
            J Offline
            Juergen_Skrotzky
            wrote on last edited by
            #5

            @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!

            1 Reply Last reply
            0
            • J Juergen_Skrotzky

              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?

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #6

              @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.

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved