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?
Servers for Qt installer are currently down

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 Offline
    S Offline
    soma_yarram
    wrote on 20 Feb 2017, 09:03 last edited by
    #1

    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.

    M C 2 Replies Last reply 20 Feb 2017, 09:12
    0
    • S soma_yarram
      20 Feb 2017, 09:03

      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.

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 20 Feb 2017, 09:12 last edited by
      #2

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

      S 1 Reply Last reply 20 Feb 2017, 11:04
      1
      • S soma_yarram
        20 Feb 2017, 09:03

        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.

        C Offline
        C Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on 20 Feb 2017, 09:17 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 20 Feb 2017, 11:14
        4
        • M mrjj
          20 Feb 2017, 09:12

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

          S Offline
          S Offline
          soma_yarram
          wrote on 20 Feb 2017, 11:04 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.

          M 1 Reply Last reply 20 Feb 2017, 11:09
          0
          • S soma_yarram
            20 Feb 2017, 11:04

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

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 20 Feb 2017, 11:09 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 20 Feb 2017, 11:21
            0
            • C Chris Kawa
              20 Feb 2017, 09:17

              @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 20 Feb 2017, 11:14 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
              • M mrjj
                20 Feb 2017, 11:09

                @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 20 Feb 2017, 11:21 last edited by
                #7

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

                kshegunovK 1 Reply Last reply 20 Feb 2017, 23:17
                0
                • S soma_yarram
                  20 Feb 2017, 11:21

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

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on 20 Feb 2017, 23:17 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 21 Feb 2017, 10:16
                  1
                  • kshegunovK kshegunov
                    20 Feb 2017, 23:17

                    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 21 Feb 2017, 10:16 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 21 Feb 2017, 11:02
                    0
                    • S soma_yarram
                      21 Feb 2017, 10:16

                      @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 21 Feb 2017, 11:02 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 23 Feb 2017, 09:39
                      2
                      • kshegunovK kshegunov
                        21 Feb 2017, 11:02

                        @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 23 Feb 2017, 09:39 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 23 Feb 2017, 09:46
                        0
                        • S soma_yarram
                          23 Feb 2017, 09:39

                          @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 23 Feb 2017, 09:46 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

                          1/12

                          20 Feb 2017, 09:03

                          • Login

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