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 manage unhandled exceptions?
Forum Updated to NodeBB v4.3 + New Features

How to manage unhandled exceptions?

Scheduled Pinned Locked Moved General and Desktop
19 Posts 3 Posters 8.3k Views 1 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #7

    The bad news is, that you need a

    @
    try {
    ...
    } catch (...) {
    ...
    }
    @

    around everything you expect to throw. The good news is, that you'll almost never need this, because Qt classes themselves don't throw.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #8

      Whenever I see
      @
      } catch (...) {
      @
      I smell a bug-report coming soon and usually I'm right. It's a very good indicator of a certain way of thinking: "I don't know what this stuff does but it crashes my app so I'll just catch anything it throws at me, ignore it, and pretend nothing happened." otherwise known as "I'll sweep it under a rug and see if anybody notices. If not then I consider it fixed".

      It's a kind of "philosophical dilemma":http://en.wikipedia.org/wiki/If_a_tree_falls_in_a_forest ;)

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #9

        .. and it's definitely a good starting point for a major flamewar :-D

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #10

          Good point. I'm shutting up now :)

          1 Reply Last reply
          0
          • X Offline
            X Offline
            xalisonx
            wrote on last edited by
            #11

            Thanks guys!
            It is not all clear.

            Let me give an example:

            @QJsonObject *o = NULL;
            qDebug()<<o->empty();@

            Ok this code is bad, I know, but crash my application!!!
            How to "catch" this kind of errors?

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #12

              Null pointer dereference is a software bug. You should not catch it. You should let it crash during testing and fix the bug. Use assert().

              If allowing a null value is part of your design (perfectly valid sometimes) then you should test the pointer before using it.

              1 Reply Last reply
              0
              • X Offline
                X Offline
                xalisonx
                wrote on last edited by
                #13

                Okay, you're right. But i'm now releasing my application. It contain a simple http server, that must be always active! I prefer to ensure that the server remains active and continuing to provide http services, although there are bugs.
                It's too weird?
                :)

                1 Reply Last reply
                0
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by
                  #14

                  So your question basically is: "I wrote a buggy software. How can I make it work without fixing it?"
                  My answer is: "Sorry, I can't help you" ;)

                  [EDIT]: To give you a little more constructive answer: If you don't have comprehensive testing suite use some static analysis tool like "cppcheck":http://cppcheck.sourceforge.net/ It's very good at finding this kind of bugs so you should be able to detect and fix them quicker.

                  1 Reply Last reply
                  0
                  • X Offline
                    X Offline
                    xalisonx
                    wrote on last edited by
                    #15

                    I do not seek a philosophical solution, but wonder if there is a technical solution.

                    1 Reply Last reply
                    0
                    • Chris KawaC Offline
                      Chris KawaC Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on last edited by
                      #16

                      The best technical solution is to fix the bugs. As I mentioned earlier - you can surround your code with try/catch and ignore such errors, but they will often lead to more serious problems like data corruption or security vulnerabilities (e.g. writing out of array bounds, uninitialized variables pointing to "secret" data and many others). Considering your software is an http server I'm guessing that's not something you want to risk.

                      In any case I wouldn't risk keeping such software running after a failure occurs. The state it's in is indeterminable. Just let it crash (log stuff if you can but crash anyway).
                      A crude workaround is to create a service that would monitor if your server is running and restart it when it crashes. It's a little like putting a band-aid on a broken leg but might be a temporary solution until you can fix the soft.

                      1 Reply Last reply
                      0
                      • X Offline
                        X Offline
                        xalisonx
                        wrote on last edited by
                        #17

                        Ok Chris, I try to explain better.
                        I'm new with Qt.
                        With other languages (ie. C#, Java, Javascript also) you can catch and manage exceptions like null pointer reference, and so on.

                        How in Qt? Is possible or not?? This is my question.

                        This code doesn't works.

                        @try {
                        QJsonObject *o = NULL;
                        qDebug()<<o->empty();
                        }
                        catch(std::exception ex) {
                        // don't catch!!!
                        }
                        catch(...) {
                        // don't catch!!!
                        }@

                        The reply may be simple:

                        1. NO, no solution for you :( Qt is unable to catch this.
                        2. YES, this is solution :) ...............
                        1 Reply Last reply
                        0
                        • Chris KawaC Offline
                          Chris KawaC Offline
                          Chris Kawa
                          Lifetime Qt Champion
                          wrote on last edited by
                          #18

                          The reply is: NOne of these apply ;) Or in other words: yes and no. You can do it but you never should.

                          bq. How in Qt? Is possible or not?? This is my question.

                          I can only repeat until you get it - Qt doesn't throw exceptions. Also, your example has nothing to do with Qt. You could change QJsonObject to Foo and it would still be the same case. The only exception thrown here would be (if it's enabled by compiler switches) by the platform runtime, not Qt.

                          There is no null pointer exception in c++ language. Dereferencing a null pointer is a software bug in c++, an undefined behavior and you can't reliably catch it (apart from platform specific runtime extensions). You should never try it because there's no virtual machine that will save you like in java or sandbox like in javascript.

                          The proper way to do this in c++ is:
                          @
                          QJsonObject* object = //get it from somewhere
                          if(object)
                          qDebug() << object->empty();
                          @

                          1 Reply Last reply
                          0
                          • X Offline
                            X Offline
                            xalisonx
                            wrote on last edited by
                            #19

                            Yay Chris, now I got the power! :) thanks for the explanation

                            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