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. Correct way to create custom exception.

Correct way to create custom exception.

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 2.4k 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.
  • F Offline
    F Offline
    federico.massimi
    wrote on last edited by
    #1

    hi, I'm writing a MapRendering class to render an image, and I'd like to add a custom exception: RenderingException.
    Searching on the internet I write to this code:

    class MapRendering {
    public:
        MapRendering(QString mapsource);
        void rendering(qreal lat, qreal lon, const QVector<uint> range_vector);
    private:
        QString map_source;
    };
    
    class RenderingException : public QException {
    public:
        RenderingException(QString const& text=" ") throw()
                :message(text) {}
        void raise() const override { throw *this; }
        RenderingException *clone() const override { return new RenderingException(*this); }
        const char* what() const throw() { return this->message.toStdString().c_str(); }
    
    private:
        QString message;
    };
    

    No problem with the class, but there are many warnings with the exception subclass of QException:
    0_1563269555325_warning.png
    someone can help me on how to write the subclass of QException correctly and without warning. Thanks in advance.

    Pl45m4P 1 Reply Last reply
    0
    • F federico.massimi

      hi, I'm writing a MapRendering class to render an image, and I'd like to add a custom exception: RenderingException.
      Searching on the internet I write to this code:

      class MapRendering {
      public:
          MapRendering(QString mapsource);
          void rendering(qreal lat, qreal lon, const QVector<uint> range_vector);
      private:
          QString map_source;
      };
      
      class RenderingException : public QException {
      public:
          RenderingException(QString const& text=" ") throw()
                  :message(text) {}
          void raise() const override { throw *this; }
          RenderingException *clone() const override { return new RenderingException(*this); }
          const char* what() const throw() { return this->message.toStdString().c_str(); }
      
      private:
          QString message;
      };
      

      No problem with the class, but there are many warnings with the exception subclass of QException:
      0_1563269555325_warning.png
      someone can help me on how to write the subclass of QException correctly and without warning. Thanks in advance.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @federico.massimi

      Hi :)

      1. Warning: I guess you have to add a destructor.
      2. Warning: Deprecated -> Compiler shows suggestion to fix it.
      3. Warning: Add override after what()

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      F 1 Reply Last reply
      2
      • Pl45m4P Pl45m4

        @federico.massimi

        Hi :)

        1. Warning: I guess you have to add a destructor.
        2. Warning: Deprecated -> Compiler shows suggestion to fix it.
        3. Warning: Add override after what()
        F Offline
        F Offline
        federico.massimi
        wrote on last edited by
        #3

        @Pl45m4
        if I add the destructor the warning on class declaration disappear, but I have this new one:

        maprendering.h:38:5: warning: definition of implicit copy constructor for 'RenderingException' is deprecated because it has a user-declared destructor
        maprendering.h:40:41: note: in implicit copy constructor for 'RenderingException' first required here
        

        0_1563272313147_W1.PNG

        Adding override after what() I have this error:

        maprendering.h:42:24: error: non-virtual member function marked 'override' hides virtual member function
        exception.h:69:5: note: hidden overloaded virtual function 'std::exception::what' declared here: different qualifiers (const vs none)
        

        0_1563272450075_W2.PNG

        jsulmJ 1 Reply Last reply
        0
        • F federico.massimi

          @Pl45m4
          if I add the destructor the warning on class declaration disappear, but I have this new one:

          maprendering.h:38:5: warning: definition of implicit copy constructor for 'RenderingException' is deprecated because it has a user-declared destructor
          maprendering.h:40:41: note: in implicit copy constructor for 'RenderingException' first required here
          

          0_1563272313147_W1.PNG

          Adding override after what() I have this error:

          maprendering.h:42:24: error: non-virtual member function marked 'override' hides virtual member function
          exception.h:69:5: note: hidden overloaded virtual function 'std::exception::what' declared here: different qualifiers (const vs none)
          

          0_1563272450075_W2.PNG

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

          @federico.massimi said in Correct way to create custom exception.:

          exception.h:69:5: note: hidden overloaded virtual function 'std::exception::what' declared here: different qualifiers

          You forgot const after method name.

          "warning: definition of implicit copy constructor for 'RenderingException' is deprecated because it has a user-declared destructor" - add a copy constructor.

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

          F 1 Reply Last reply
          2
          • jsulmJ jsulm

            @federico.massimi said in Correct way to create custom exception.:

            exception.h:69:5: note: hidden overloaded virtual function 'std::exception::what' declared here: different qualifiers

            You forgot const after method name.

            "warning: definition of implicit copy constructor for 'RenderingException' is deprecated because it has a user-declared destructor" - add a copy constructor.

            F Offline
            F Offline
            federico.massimi
            wrote on last edited by federico.massimi
            #5

            @jsulm said in Correct way to create custom exception.:

            const

            Every time i remove a warning a new one appear, now my code is:

            class RenderingException : public QException
            {
            public:
                RenderingException(QString const& text=" ") noexcept
                        :message(text) {}
                RenderingException(const RenderingException &re) {this->message = re.message; }
                ~RenderingException() override {}
            
                void raise() const override { throw *this; }
                RenderingException *clone() const override { return new RenderingException(*this); }
                const char *what() const noexcept override { return this->message.toStdString().c_str(); }
            
            private:
                QString message;
            };
            

            and I have just one warning on the first line (class RenderingException : public QException):

            maprendering.h:33:7: warning: 'RenderingException' has no out-of-line virtual method definitions; its vtable will be emitted in every translation unit
            
            1 Reply Last reply
            0
            • M Offline
              M Offline
              markus.o
              wrote on last edited by
              #6

              You could move any (or all) of your function definitions (or the constructor) into a seperate .c or .cpp file to make the warning disappear. The warning is emitted because your class (unrelated to whether its an exception or not) doesnt have it's own compilation unit (all definitions and declarations are in the header), so it's vtable is copied in every unit that uses your class.

              1 Reply Last reply
              3
              • F Offline
                F Offline
                federico.massimi
                wrote on last edited by
                #7

                OK, Work!

                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