开发者

How to catch exceptions with Qt platform independently?

开发者 https://www.devze.com 2023-01-30 22:15 出处:网络
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 catch

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();
  priva开发者_高级运维te:
    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();
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消