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. How to catch all exceptions, even without throw in Qt project?
Forum Updated to NodeBB v4.3 + New Features

How to catch all exceptions, even without throw in Qt project?

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 14.1k Views 4 Watching
  • 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.
  • S soma_yarram

    Hello,

    I'm trying to build a test project in Qt Creator, where I want to catch all exceptions in single catch block even without throw.

    For example, the following code works in MS Visual Studio without crash, if I set the option C/C++ -> Code Generation -> Enable C++ Exceptions to /EHa:

    int a = 100, b = 0, c = 1;
    try
    {
        c = a / b;
    }
    catch (...)
    {
        c = 0;
    }
    

    But the same code above crashes in Qt Creator (as expected). Is there any QMake flag or option (similar to /EHa option in MS Visual Studio) I can set in Qt .pro file, so that I can avoid crash and catch all exceptions without throw?

    Thanks in advance.

    Chris KawaC Offline
    Chris KawaC Offline
    Chris Kawa
    Lifetime Qt Champion
    wrote on last edited by
    #3

    @soma_yarram said in How to catch all exceptions, even without throw in Qt project?:

    Is there any QMake flag or option (similar to /EHa option in MS Visual Studio) I can set in Qt .pro file, so that I can avoid crash and catch all exceptions without throw?

    /EHa is a compiler flag. You can pass compiler flags via qmake in the .pro file like this:

    QMAKE_CXX_FLAGS += /EHa
    
    S 1 Reply Last reply
    4
    • mrjjM mrjj

      @soma_yarram
      Hi
      Maybe something like this
      Menu: Tools->Options->Debugger
      alt text

      S Offline
      S Offline
      soma_yarram
      wrote on last edited by
      #4

      @mrjj Thanks for the reply, but I don't see any CDB tab in my Debugger option. Moreover, I don't think it is related to debugger, I want to have a compiler option which can treat system level errors (e.g. divide-by-zero) as a normal exception.

      mrjjM 1 Reply Last reply
      0
      • S soma_yarram

        @mrjj Thanks for the reply, but I don't see any CDB tab in my Debugger option. Moreover, I don't think it is related to debugger, I want to have a compiler option which can treat system level errors (e.g. divide-by-zero) as a normal exception.

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #5

        @soma_yarram
        Hi
        I thought of debugging. sorry.

        Well Chris shows how to add the flag

        but I its unclear which compiler , you are using ?

        S 1 Reply Last reply
        0
        • Chris KawaC Chris Kawa

          @soma_yarram said in How to catch all exceptions, even without throw in Qt project?:

          Is there any QMake flag or option (similar to /EHa option in MS Visual Studio) I can set in Qt .pro file, so that I can avoid crash and catch all exceptions without throw?

          /EHa is a compiler flag. You can pass compiler flags via qmake in the .pro file like this:

          QMAKE_CXX_FLAGS += /EHa
          
          S Offline
          S Offline
          soma_yarram
          wrote on last edited by
          #6

          @Chris-Kawa Thanks for the reply, but it doesn't work. App still crashes while dividing by zero.

          1 Reply Last reply
          0
          • mrjjM mrjj

            @soma_yarram
            Hi
            I thought of debugging. sorry.

            Well Chris shows how to add the flag

            but I its unclear which compiler , you are using ?

            S Offline
            S Offline
            soma_yarram
            wrote on last edited by
            #7

            @mrjj I use GCC compiler under linux and mac platforms.

            kshegunovK 1 Reply Last reply
            0
            • S soma_yarram

              @mrjj I use GCC compiler under linux and mac platforms.

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #8

              You assume division by zero is an exception. As it happens it's an error that the ALU encounters when trying to execute the corresponding asm instruction and how that's handled depends on the OS, but usually (i.e. with the exception of Windows) it is not an exception. What one usually does to protect the code is to add assertions, so such errors can be caught at debug time, e.g.:

              Q_ASSERT(b);
              c = a / b;
              

              Read and abide by the Qt Code of Conduct

              S 1 Reply Last reply
              1
              • kshegunovK kshegunov

                You assume division by zero is an exception. As it happens it's an error that the ALU encounters when trying to execute the corresponding asm instruction and how that's handled depends on the OS, but usually (i.e. with the exception of Windows) it is not an exception. What one usually does to protect the code is to add assertions, so such errors can be caught at debug time, e.g.:

                Q_ASSERT(b);
                c = a / b;
                
                S Offline
                S Offline
                soma_yarram
                wrote on last edited by
                #9

                @kshegunov I agree to your statement. I know it is safe to check the values before doing anything, but I just want to know any flag/option (similar to /EHa in MS Visual Studio) I can set in Qt .pro file, which is loaded by IDE Qt Creator (using GCC compiler) under linux and mac platforms, which can avoid crash, treat system error as a normal exception, and execute catch block immediately. Any help would be much appreciated. Thanks.

                kshegunovK 1 Reply Last reply
                0
                • S soma_yarram

                  @kshegunov I agree to your statement. I know it is safe to check the values before doing anything, but I just want to know any flag/option (similar to /EHa in MS Visual Studio) I can set in Qt .pro file, which is loaded by IDE Qt Creator (using GCC compiler) under linux and mac platforms, which can avoid crash, treat system error as a normal exception, and execute catch block immediately. Any help would be much appreciated. Thanks.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #10

                  @soma_yarram said in How to catch all exceptions, even without throw in Qt project?:

                  but I just want to know any flag/option (similar to /EHa in MS Visual Studio) I can set in Qt .pro file, which is loaded by IDE Qt Creator (using GCC compiler) under linux and mac platforms

                  The point is, there's no such flag! What you should do is check your value before dividing by it. And by the way dividing something by zero is undefined, so your example code is badly posed even in the case there's an exception thrown.

                  Read and abide by the Qt Code of Conduct

                  S 1 Reply Last reply
                  2
                  • kshegunovK kshegunov

                    @soma_yarram said in How to catch all exceptions, even without throw in Qt project?:

                    but I just want to know any flag/option (similar to /EHa in MS Visual Studio) I can set in Qt .pro file, which is loaded by IDE Qt Creator (using GCC compiler) under linux and mac platforms

                    The point is, there's no such flag! What you should do is check your value before dividing by it. And by the way dividing something by zero is undefined, so your example code is badly posed even in the case there's an exception thrown.

                    S Offline
                    S Offline
                    soma_yarram
                    wrote on last edited by
                    #11

                    @kshegunov Thanks for the comment. Divide-by-zero is just an example I gave. One more example can be accessing invalid pointers as below:

                    QWidget *pWidget;
                    
                    try
                    {
                        if (pWidget)
                        {
                            pWidget->setObjectName("Hello");
                        }
                    }
                    catch (...)
                    {
                        std::cout << "something went wrong";
                    }
                    

                    As the pWidget points to invalid location, and the check if (pWidget) succeeds, the code above definitely crashes. I know that initializing pointer pWidget to NULL would solve the issue, but the crash can be avoided with MCVC compiler flag /EHa, and I only want to know the corresponding flag for GCC compiler.

                    kshegunovK 1 Reply Last reply
                    0
                    • S soma_yarram

                      @kshegunov Thanks for the comment. Divide-by-zero is just an example I gave. One more example can be accessing invalid pointers as below:

                      QWidget *pWidget;
                      
                      try
                      {
                          if (pWidget)
                          {
                              pWidget->setObjectName("Hello");
                          }
                      }
                      catch (...)
                      {
                          std::cout << "something went wrong";
                      }
                      

                      As the pWidget points to invalid location, and the check if (pWidget) succeeds, the code above definitely crashes. I know that initializing pointer pWidget to NULL would solve the issue, but the crash can be avoided with MCVC compiler flag /EHa, and I only want to know the corresponding flag for GCC compiler.

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by kshegunov
                      #12

                      @soma_yarram said in How to catch all exceptions, even without throw in Qt project?:

                      I only want to know the corresponding flag for GCC compiler.

                      There's none. Segmentation fault (what dereferencing dangling pointers cause) can be caught by trapping the corresponding POSIX signal, but that's a last resort measure and usually in the handler one puts ::exit() to terminate the application. This signal is not raised for catching bad code, but as a means to do final cleanup (like closing driver handles and the such) before the runtime terminates the application.

                      Read and abide by the Qt Code of Conduct

                      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