Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Subclassing QException

    General and Desktop
    qexception
    2
    2
    1780
    Loading More Posts
    • 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
      samuelpdrouin last edited by samuelpdrouin

      Hi,

      I want to subclass QException in order to handle QSqlError. My current exception handling code is the following :

      try
      {
          query.exec(queryString);
          throw query.lastError().text();
      }
      catch (QString &e)
      {
          if (!e.isEmpty())
          {
              bool ret = QERMessageBox(e);
              if (ret = true)
                  // Code
          }
      }
      

      I thought of reimplanting QException like this :

      class SqlException: public QException
      
      {
      public:
          SqlException(QString const& text=" ") throw()
               :m_phrase(text)
          {}
           virtual const QChar* what() const throw()
           {
               return m_text.data();
           }
           void SqlException::raise() const
          {
               SqlException e = *this;
               throw e;
          }
          SqlException *SqlException::clone() const
          {
               return new SqlException(*this);
          }
           virtual ~Erreur() throw()
          {}
      
      private:
          string m_text; \\ QSqlQuery.lastError().text();
      };
      

      However I don't understand the use of the raise() and the clone() function. Also, I get the following error from the what function when compiling : return cannot convert from const QChat * to const char *.

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        Raise is to actually throw the exception and clone is to create a copy of your exception. As for your other error m_text is a string and not a QString.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • First post
          Last post