如何使用Qt平台独立捕获异常?

hox*_*nox 3 c++ qt cross-platform exception-handling exception

我在项目中使用boost :: date_time。如果日期无效,它将阻止std :: out_of_range C ++异常。在Windows平台上的Qt的gui应用程序中,它成为SEH例外,因此它不会被try | catch范例和编程模版所困扰。如何独立捕获异常平台?

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

编辑: 如果有人不理解,我写了另一个例子:

Qt专业版文件:

TEMPLATE = app
DESTDIR  = bin
VERSION  = 1.0.0
CONFIG  += debug_and_release build_all
TARGET = QExceptExample
SOURCES += exceptexample.cpp \
           main.cpp
HEADERS += exceptexample.h
Run Code Online (Sandbox Code Playgroud)

除了example.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
Run Code Online (Sandbox Code Playgroud)

除了example.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);
  }
}
Run Code Online (Sandbox Code Playgroud)

main.cpp

#include "exceptexample.h"
#include <QApplication>

int main(int argc, char* argv[])
{
  QApplication app(argc, argv);
  ExceptExample e;
  e.show();
  return app.exec();
}
Run Code Online (Sandbox Code Playgroud)

Mar*_* Ba 5

从评论中添加答案:

aschelper写道:

是否启用了使用C ++异常支持编译的Qt库?有时它们不是,这会导致问题。

hoxnox(OP)回答:

@aschelper我用-exceptions选项重新配置了Qt。它固定的情况。如果您发布答案,我会将其标记为正确。