Results 1 to 7 of 7

Thread: Boost-like exceptions

  1. #1
    Join Date
    Jul 2017
    Posts
    37
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Boost-like exceptions

    My program, so far, has been throwing ints as errors. I made it pop up a message if it failed to make a TIN, so I need to change this. The Boost exception class has a way of adding info to an exception as it is thrown up the call stack. For instance, a CSV line-parsing routine can throw an exception if a line has an odd number of quotation marks, and the next function up can add the filename to the exception. The Qt exception class doesn't do any such thing. Can it be added to Qt exception, or should I use the Boost exception?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Boost-like exceptions

    Can it be added to Qt exception,
    Qt is written in C++ so if you know what inheritance is, you should know it possible to add that functionality to QException.
    More over the docs of QException state it in the very first sentence:
    The QException class provides a base class for exceptions that can transferred across threads.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jul 2017
    Posts
    37
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Boost-like exceptions

    I can pretty easily derive a class from QException that carries a payload, where the maximum payload size is known in advance (e.g. three ints and two pointers). I don't know how to stuff arbitrary-length strings into an exception without looking at how Boost does it. The problem is, if an out-of-memory occurs deep in the code, and somewhere up the stack a function tries to stuff a filename into the exception, that'll cause an out-of-memory exception while handling an out-of-memory exception, which is a no-no.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Boost-like exceptions

    I am sorry, but I don't follow.
    What does your last post has to do with the first?
    Anyway, if you use QString you don't have to worry about the length of the string.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jul 2017
    Posts
    37
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Boost-like exceptions

    I was referring to the following sentence from the Boost site:
    It supports transporting of arbitrary data to the catch site, which is otherwise tricky due to the no-throw requirements (15.5.1) for exception types.
    Anyway, I'm trying to implement my exception type, looking at https://doc.qt.io/qt-5/qexception.html, and running into a problem. clone does new, which calls the copy constructor, but QException does not have a copy constructor! Do I just make my own, without calling QException's?
    Qt Code:
    1. class BeziExcept: public QException
    2. {
    3. public:
    4. BeziExcept(BeziExcept a);
    5. BeziExcept(int num);
    6. int exceptNumber;
    7. int pointNumber[2];
    8. void raise() const
    9. {
    10. throw *this;
    11. }
    12. BeziExcept *clone() const
    13. {
    14. return new BeziExcept(*this);
    15. }
    16. };
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Boost-like exceptions

    Now that I had a the possibility to read your second post with a bit more attention...
    The problem is, if an out-of-memory occurs deep in the code, and somewhere up the stack a function tries to stuff a filename into the exception, that'll cause an out-of-memory exception while handling an out-of-memory exception, which is a no-no.
    Well, I am not sure what you at all could you do in such a state since most things you would like to do would need memory.
    Also, are you differentiating between stack and heap memory?
    One way to deal with it is to allocate some memory at the start of the application (or before the usage of this exception), and hold it for your out of memory exception, and delete it just before you want to allocate your exception.
    You could initialize a pre sized QByteArray as a placeholder for your string in your exception, so that you know your new exception will not be larger than the amount of memory you allocated as reserve.

    clone does new, which calls the copy constructor, but QException does not have a copy constructor! Do I just make my own, without calling QException's?
    well, if QException doesn't have one, you can't call it ;-)
    And I'd say yes, I see no reason why you could not implement your own copy constructor.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Boost-like exceptions

    As far as I can tell the QException class will have a default copy constructor. The implementation of QUnhandledException in the standard library does exactly what the docs describe.

    Your BeziExcept code declares a conversion constructor (roughly looks like a copy constructor but does not have the requisite signature) but does not provide an implementation. Is that the error you see? Since the members are all basic types you could probably just omit line 4 altogether and rely on the default copy constructor, or you could implement it explicitly:
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. class BeziExcept: public QException {
    5. public:
    6. // default and conversion constructor
    7. BeziExcept(int num = 0):
    8. exceptNumber(num),
    9. pointNumber{0, 0}
    10. {
    11. }
    12. // Copy constructor
    13. BeziExcept(const BeziExcept &a) {
    14. exceptNumber = a.exceptNumber;
    15. pointNumber[0] = a.pointNumber[0];
    16. pointNumber[1] = a.pointNumber[1];
    17. }
    18. int exceptNumber;
    19. int pointNumber[2];
    20. void raise() const
    21. {
    22. throw *this;
    23. }
    24. BeziExcept *clone() const
    25. {
    26. return new BeziExcept(*this);
    27. }
    28. };
    29.  
    30.  
    31. int main(int argc, char **argv) {
    32. QCoreApplication app(argc, argv);
    33.  
    34. BeziExcept a(314);
    35. qDebug() << "a" << a.exceptNumber;
    36. BeziExcept b(a);
    37. qDebug() << "b" << b.exceptNumber;
    38. BeziExcept *c = b.clone();
    39. qDebug() << "c" << c->exceptNumber;
    40.  
    41. return( 0 );
    42. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. gcc exceptions
    By ctarsoaga in forum Qt Programming
    Replies: 0
    Last Post: 12th April 2010, 20:58
  2. Using exceptions with Qt
    By vu64 in forum Qt Programming
    Replies: 1
    Last Post: 17th May 2009, 03:28
  3. sql Exceptions
    By peace_comp in forum Qt Programming
    Replies: 2
    Last Post: 10th October 2008, 02:51
  4. QSA exceptions
    By seneca in forum Qt Programming
    Replies: 2
    Last Post: 2nd February 2006, 15:27
  5. QT and exceptions
    By krivenok in forum Qt Programming
    Replies: 4
    Last Post: 25th January 2006, 14:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.