How to catch exceptions with Qt platform independently?

10,166

Adding answer from the comments:

aschelper wrote:

Is your Qt library compiled with C++ exception support enabled? Sometimes they're not, which causes problems.

hoxnox (OP) answered:

@aschelper I reconfigured Qt with -exceptions option. It fixed situation. If you'll post the answer I'll mark it as right.

Share:
10,166
hoxnox
Author by

hoxnox

Updated on June 04, 2022

Comments

  • hoxnox
    hoxnox almost 2 years

    I'm using boost::date_time in my project. When date is not valid it thorws std::out_of_range C++ exception. In Qt's gui application on windows platform it becomes SEH exception, so it doesn't catched with try|catch paradigm and programm dies. How can I catch the exception platform independently?

    try{
        std::string ts("9999-99-99 99:99:99.999");
        ptime t(time_from_string(ts))
    }
    catch(...)
    {
        // doesn't work on windows
    }
    

    EDITED: If somebody didn't understand, I wrote another example:

    Qt pro file:

    TEMPLATE = app
    DESTDIR  = bin
    VERSION  = 1.0.0
    CONFIG  += debug_and_release build_all
    TARGET = QExceptExample
    SOURCES += exceptexample.cpp \
               main.cpp
    HEADERS += exceptexample.h
    

    exceptexample.h

    #ifndef __EXCEPTEXAMPLE_H__
    #define __EXCEPTEXAMPLE_H__
    
    #include <QtGui/QMainWindow>
    #include <QtGui/QMessageBox>
    #include <QtGui/QPushButton>
    #include <stdexcept>
    
    class PushButton;
    class QMessageBox;
    
    class ExceptExample : public QMainWindow
    {
      Q_OBJECT
      public:
        ExceptExample();
        ~ExceptExample();
      public slots:
        void throwExcept();
      private:
        QPushButton * throwBtn;
    };
    
    #endif
    

    exceptexample.cpp

    #include "exceptexample.h"
    
    ExceptExample::ExceptExample()
    {
      throwBtn = new QPushButton(this);
      connect(throwBtn, SIGNAL(clicked()), this, SLOT(throwExcept()));
    }
    
    ExceptExample::~ExceptExample()
    {
    }
    
    void ExceptExample::throwExcept()
    {
      QMessageBox::information(this, "info", "We are in throwExcept()", 
                               QMessageBox::Ok);
      try{
        throw std::out_of_range("ExceptExample");
      }
      catch(...){
        QMessageBox::information(this, "hidden", "Windows users can't see "
                                 "this message", QMessageBox::Ok);
      }
    }
    

    main.cpp

    #include "exceptexample.h"
    #include <QApplication>
    
    int main(int argc, char* argv[])
    {
      QApplication app(argc, argv);
      ExceptExample e;
      e.show();
      return app.exec();
    }