Results 1 to 10 of 10

Thread: How to add simple custom exception handler?

  1. #1
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to add simple custom exception handler?

    Hi everyone. I would like to create a custom class for known exception (extending QtConcurrent::Exception), but unfortunately I'm missing a few puzzles... Here is what I have s far illustrating what I would like to achieve.
    Qt Code:
    1. class myException: public QtConcurrent::Exception {
    2. public:
    3. myException(const char *desc) {
    4. QMessageBox::critical(NULL,QObject::tr("Warning"),QObject::tr(desc));
    5. }
    6.  
    7. ~myException() throw();
    8.  
    9. };
    To copy to clipboard, switch view to plain text mode 
    in a function I try to raise exception like this:
    Qt Code:
    1. int b = 0;
    2. if (b == 0)
    3. myException *exc = new myException("division with 0!");
    To copy to clipboard, switch view to plain text mode 
    but can't make it compile, I get:
    error: undefined reference to `vtable for myException'. Can you please correct this sample so that it works, much thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to add simple custom exception handler?

    You declare a destructor for myException but you are not defining it (missing body).

    Also using a message box in the excepion's constructor does not make sense, especially since this is concurrent exception.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to add simple custom exception handler?

    You declare a destructor for myException but you are not defining it (missing body).
    ok I probably don't need a destructor in my case
    Also using a message box in the excepion's constructor does not make sense, especially since this is concurrent exception.
    well it may be a bad idea, however I would like to show a message with description of what went wrong to the user, should I do it some other way? Please provide a short sample code.. I've changed my sample like this:
    Qt Code:
    1. class myException: public QtConcurrent::Exception {
    2. public:
    3. myException(const char *desc) {
    4. //QMessageBox::critical(NULL,QObject::tr("Warning"),QObject::tr(desc));
    5. qDebug() << desc;
    6. }
    7. };
    To copy to clipboard, switch view to plain text mode 
    and the calling function
    Qt Code:
    1. myException exc("division with 0");
    2. throw exc;
    To copy to clipboard, switch view to plain text mode 
    Which works so so..., if I leave the messagebox, this gets displayed but soon afterwards the whole app crashes with something like "Microsoft Visual C++ Runtime Library: This application has requested the Runtime to terminate in an unusual way." If I use qDebug instead, the message is displayed only in "application output window", but still crashes the same way. I guesss I'm doing something wrong here, please show the right direction, a snippet of code would be useful, thanks.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to add simple custom exception handler?

    Quote Originally Posted by arcull View Post
    however I would like to show a message with description of what went wrong to the user, should I do it some other way?
    Handling an exception is what the catch block is for.

    How does your's look?

    Cheers,
    _

  5. #5
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to add simple custom exception handler?

    I was trying to handle exceptions in few functions and forgot to add a try-catch block in one of them, that's why my app crashed with "Microsoft Visual C++ Runtime Library:blah blah".My working sample now looks like this:
    Qt Code:
    1. class myException: public QtConcurrent::Exception {
    2. public:
    3. myException(const char *desc) {
    4. excdesc = desc;
    5. }
    6. const char *excdesc;
    7. };
    To copy to clipboard, switch view to plain text mode 
    and a part of one function
    Qt Code:
    1. try {
    2. //some other code...
    3. int b = 0;
    4. if (b == 0) {
    5. myException exc("you can not divide with 0");
    6. throw exc;
    7. }
    8. } catch (myException & e) {
    9. QMessageBox::critical(this,tr("Warning"),tr(e.excdesc));
    10. }
    To copy to clipboard, switch view to plain text mode 
    which works ok, however I see little point have an extra class myException for as little as showing a simple error description. Couldn't I do just something like this:
    Qt Code:
    1. try {
    2. //some other code...
    3. int b = 0;
    4. if (b == 0) {
    5. throw "you can not divide with 0";
    6. }
    7. } catch (char *e) {
    8. QMessageBox::critical(this,tr("Warning"),tr(e));
    9. }
    To copy to clipboard, switch view to plain text mode 
    but my app crashes this way. Please suggest, thanks.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to add simple custom exception handler?

    QtConcurrent::Exception is for throwing expections in a threadpool worker thread but catching it in the calling thread (usually the main thread).

    If all you need is a thread local exception, just use std::exception http://www.cplusplus.com/reference/exception/exception/

    Cheers,
    _

  7. #7
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to add simple custom exception handler?

    Quote Originally Posted by anda_skoa View Post
    QtConcurrent::Exception is for throwing expections in a threadpool worker thread but catching it in the calling thread (usually the main thread).

    If all you need is a thread local exception, just use std::exception http://www.cplusplus.com/reference/exception/exception/

    Cheers,
    _
    Thanks I agree, but currently I just don't see how to pass a custom error message to the user this way.
    Qt Code:
    1. QMessageBox::critical(this,tr("Warning"),tr(e.what()));
    To copy to clipboard, switch view to plain text mode 
    but how do I put custom string to "what" when throwing exceptions. Thanks again.

  8. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to add simple custom exception handler?

    Qt Code:
    1. how do I put custom string to "what" when throwing exceptions
    To copy to clipboard, switch view to plain text mode 
    via constructor
    Qt Code:
    1. void fn(){
    2. throw std::runtime_error("this is my message");
    3. }
    4.  
    5. or
    6.  
    7. class my_exception : public std::exception{
    8. public:
    9. my_exception(const QString& string){
    10. _data = string.toLocal8it();
    11. }
    12. const char * what() const noexcept{
    13. return _data.constData();
    14. }
    15. private:
    16. QByteArray _data;
    17. };
    18. ...
    19. void fn(){
    20. throw my_exception(QObject::tr("this is my localized message!"));
    21. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to add simple custom exception handler?

    Simply reimplement the virtual what method of your exception subclass.
    You can also extend the constructor to take an error message, store it and return it in the what reimplementation (like in std::logic_error).

  10. #10
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to add simple custom exception handler?

    Thanks both, ok I'll have to create my custom class extending std::exception. Good, case solved

Similar Threads

  1. Gui Event Handler Exception : Illegal Escape Sequence
    By subagha in forum Qt Programming
    Replies: 0
    Last Post: 4th November 2013, 14:43
  2. Replies: 0
    Last Post: 23rd October 2013, 12:36
  3. Replies: 0
    Last Post: 9th September 2009, 02:35
  4. Replies: 12
    Last Post: 5th July 2009, 16:03
  5. Simple custom widget won't size properly
    By MrGarbage in forum Qt Tools
    Replies: 2
    Last Post: 9th August 2007, 13:12

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.