【发布时间】:2010-12-15 10:51:37
【问题描述】:
我在我的项目中使用 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
}
已编辑: 如果有人不明白,我再写一个例子:
Qt pro 文件:
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();
}
【问题讨论】:
-
它和Qt有什么关系?您没有在代码中使用任何 Qt 类。
-
该错误在 Qt 中是实际的。除了 Qt-windows-gui 之外,此代码工作正常,因为我认为它会将 std C++ 异常重新抛出为 SEH,因此应用程序会失败(catch 不起作用)。
-
我也不明白这与 Qt 有什么关系。您有抛出异常的本地非 Qt 代码,以及围绕它的 try catch 块。也 catch(...) 应该抓住一切。 Qt 的唯一问题是如果异常最终出现在 Qt 事件循环中,但这里不是这种情况。什么是 SEH?
-
@Frank Osterfeld catch(...) 不应该捕获 SEH。请参阅结构化异常处理。
-
您的 Qt 库编译时是否启用了 C++ 异常支持?有时它们不是,这会导致问题。
标签: c++ qt exception exception-handling cross-platform