Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Class object thrown as exception
Forum Updated to NodeBB v4.3 + New Features

Class object thrown as exception

Scheduled Pinned Locked Moved Solved C++ Gurus
4 Posts 2 Posters 1.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.
  • E Offline
    E Offline
    Eligijus
    wrote on last edited by
    #1

    Hello,

    I want to throw my created class Error object and catch it. Is this valid?
    Example code:

    #include <QCoreApplication>
    #include <QDebug>
    
    class Error
    {
    public:
        enum Type {
            ONE,
            TWO,
            THREE
        };
    
        Error() {}
        Error(Type type, const QString &item)
            : mType(type), mItem(item) {}
    
        Type type() const { return mType; }
        QString item() const { return mItem; }
    
    private:
        Type mType;
        QString mItem;
    };
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        try {
            throw Error(Error::TWO, "bla");
        } catch (const Error &e) {
            qDebug() << e.type() << e.item();
        }
    
        return a.exec();
    }
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      I can't see why not

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      E 1 Reply Last reply
      1
      • VRoninV VRonin

        I can't see why not

        E Offline
        E Offline
        Eligijus
        wrote on last edited by
        #3

        @VRonin Thanks. I was concerned about using QString inside class Error and Error not deriving from some base exception class. Because in pure c++ people usually don't throw std::string as exception because std::string can throw exceptions as well. For that reason custom exceptions derive from std::exception.

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          If you are using mutiple threads you can use QException base class. For the rest, Qt code itself should not throw in release code except probably for the unavoidable std::bad_alloc

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          3

          • Login

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