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. Problem exceptions from static library
Forum Updated to NodeBB v4.3 + New Features

Problem exceptions from static library

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 998 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.
  • A Offline
    A Offline
    AndrzejB
    wrote on last edited by AndrzejB
    #1

    In static library I have defined:

    #include <QException>
    namespace syntaxhl {
    class SynException: public QException
    {
    protected:
        int code;
        int level;
        QString _msg;
    public:
        explicit SynException(const char* message, int code=0, int level=0);
        explicit SynException(const QString& message, int code=0, int level=0);
        virtual const char* what() const noexcept override;
        int getCode() noexcept;
        int getLevel() noexcept;
        void raise() const override;
        SynException *clone() const override;
    };
    }
    

    In main module I have:

    repository = nullptr;
        try {
            repository = new syntaxhl::Repository(config.pathToSyntax, config.pathToThemes);
        } catch (syntaxhl::SynException &e) {
            delete repository;
            QMessageBox::warning(nullptr, "Warning", e.what(),
                                            QMessageBox::Ok);
            repository = new syntaxhl::Repository("", "");
        }
    

    and in messageBox are trash, but if I set static library as main project and set program as my caller, it will be .. ok (!)[but only for Windows, with Linux still errors], only ok in this case, if I call program directly from WIndows are errors.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AndrzejB
      wrote on last edited by
      #2

      QException has no message... and I must subclass it

      1 Reply Last reply
      0
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @AndrzejB said in Problem exceptions from static library:

        and in messageBox are trash, but if I set static library as main project and set program as my caller, it will be .. ok (!)[but only for Windows, with Linux still errors], only ok in this case, if I call program directly from WIndows are errors.

        Can you explain what exactly does not work? 'errors' is not really useful...
        And maybe a simple test app to show the problem.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1
        • A Offline
          A Offline
          AndrzejB
          wrote on last edited by
          #4

          Old good solution: instead QString, I use char buffer + new/delete + strcpy:

          #ifndef EXCEPTION_H
          #define EXCEPTION_H
          
          #include <QException>
          
          namespace syntaxhl {
          
          class SynException: public QException
          {
          protected:
              int code;
              int level;
              /** Error message.     */
              char *msgbuf;
          public:    
              /** Constructor (C++ STL strings).
               *  @param message The error message.
               */
              explicit SynException(const QString& message, int code=0, int level=0);
          
              ~SynException() override;
          
              /** Returns a pointer to the (constant) error description.
               *  @return A pointer to a const char*. The underlying memory
               *          is in posession of the Exception object. Callers must
               *          not attempt to free the memory.
               */
              virtual const char* what() const noexcept override;
          
          
              int getCode() noexcept;
          
              int getLevel() noexcept;
          
              void raise() const override;
              SynException *clone() const override;
          };
          
          }
          
          #endif // EXCEPTION_H
          
          
          #include "synexception.h"
          
          using namespace syntaxhl;
          
          SynException::SynException(const QString& message, int code, int level):code(code),level(level)
            {
              int len = message.length();
              msgbuf = new char[size_t(len) + 1];
              if (!msgbuf) return;
              strcpy(msgbuf, message.toStdString().c_str());
            }
          
          SynException::~SynException()
          {
              delete msgbuf;
          }
          
          const char* SynException::what() const noexcept{
             return msgbuf;
          }
          
          int SynException::getCode() noexcept {
             return code;
          }
          
          int SynException::getLevel() noexcept {
             return code;
          }
          
          void SynException::raise() const { throw *this; }
          
          SynException *SynException::clone() const { return new SynException(*this); }
          
          
          1 Reply Last reply
          0
          • A Offline
            A Offline
            AndrzejB
            wrote on last edited by
            #5

            Maybe my problem was bacause I return previously for _msg of type QString:
            _msg.toStdString().c_str()
            and toStdString() makes temporal std-string which was canceled before function returns?
            If std-string will class field, will be Ok?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              Out of curiosity, why are you returning a char * since the way you use it, the return value will be again changed to a QString ?

              On a side, note, you are correct, you were returning a pointer to temporary data.

              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
              1
              • A Offline
                A Offline
                AndrzejB
                wrote on last edited by AndrzejB
                #7

                Because standard virtual Exception method what() returns char*.
                Maybe I don'y override what() but write second similar method returning QString?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  The return type doesn't enter in the method signature therefore you can't have two methods with the same parameters and different return types.

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

                  A 1 Reply Last reply
                  1
                  • SGaistS SGaist

                    The return type doesn't enter in the method signature therefore you can't have two methods with the same parameters and different return types.

                    A Offline
                    A Offline
                    AndrzejB
                    wrote on last edited by
                    #9

                    I can write method QString mywhat() but it would be visible only with my type. Better is using what() returning char* because I can catch all std::exception , not only QException and MyException

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      No problem with that. As written, it was just curiosity.

                      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
                      0

                      • Login

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