Qt / C ++错误处理 [英] Qt/C++ Error handling

查看:169
本文介绍了Qt / C ++错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做很多关于用Qt / C ++处理错误的研究,而且我仍然像我开始时一样迷失。也许我正在寻找一个简单的方法(像其他语言提供)。一个,特别是提供一个我正在宗教地使用的未处理的异常。当程序遇到问题时,它会抛出未处理的异常,以便我可以创建自己的错误报告。该报告从我的客户机发送到在线服务器,然后我稍后阅读。



我使用C ++的问题是,完成的任何错误处理都以前想到(认为尝试/捕获或大规模条件)。根据我的经验,代码中的问题不在之前,否则开始就不会有问题。



编写跨平台应用程序,平台错误处理/报告/跟踪机制对我来说有些可怕。



我的问题是:是否有任何类型的Qt或C ++特定的全部错误捕获机制,我可以在我的应用程序中使用,所以如果有什么问题,我至少可以在崩溃之前写一个报告?



示例:

  
class MainWindow:public QMainWindow
{
[...]

public slots:
void add_clicked();
}

void MainWindow :: add_clicked()
{
QFileDialog dlg(this,Qt :: Sheet);
QString filename = dlg.getOpenFileName(this);

if(!filename.isEmpty())
{
QStringList path = filename.split(QDir :: separator());
QString file = path.at(path.count()); //索引超出范围声明。

if(!lst_tables-> openDatabase(filename))
{
[...]
}
}
}

我希望这个错误被捕获为一个未处理的异常,并且应用程序退出而不显示用户默认的崩溃Windows / Mac操作系统上的窗口。

解决方案

覆盖 QCoreApplication :: notify(),并在那里添加try-catch。那个,和main()中的东西涵盖了我的经验中的大多数情况。



这是一个我怎么做的。请注意,我在这里使用C ++ RTTI,而不是Qt的版本,但这只是为了方便我们的应用程序。此外,我们提供了一个QMessageBox的信息和一个链接到我们的日志文件。你应该根据自己的需要扩展。

  bool QMyApplication :: notify(QObject * receiver,QEvent * even)
{
try {
return QApplication :: notify(receiver,event);
} catch(std :: exception& e){
qFatal(Error%s send event%s to object%s(%s),
e.what() typeID(* event).name(),qPrintable(receiver-> objectName()),
typeid(* receiver).name());
} catch(...){
qFatal(Error< unknown>将事件%s发送到对象%s(%s),
typeid(* event).name ),qPrintable(receiver-> objectName()),
typeid(* receiver).name());
}

// qFatal aborts,所以这并不是真的必要的
//但是如果您使用不同的日志记录lib
return false;
}

此外,我们在Windows上使用__try,__except来捕获异步异常违规)。 Google Breakpad可能可以作为跨平台的替代品。


I've been doing a lot of research about handling errors with Qt/C++ and I'm still as lost as when I started. Maybe I'm looking for an easy way out (like other languages provide). One, in particular, provides for an unhandled exception which I use religiously. When the program encounters a problem, it throws the unhandled exception so that I can create my own error report. That report gets sent from my customers machine to a server online which I then read later.

The problem that I'm having with C++ is that any error handling that's done has to be thought of BEFORE hand (think try/catch or massive conditionals). In my experience, problems in code are not thought of before hand else there wouldn't be a problem to begin with.

Writing a cross-platform application without a cross-platform error handling/reporting/trace mechanism is a little scary to me.

My question is: Is there any kind of Qt or C++ Specific "catch-all" error trapping mechanism that I can use in my application so that, if something does go wrong I can, at least, write a report before it crashes?

Example:


class MainWindow: public QMainWindow
{
[...]

public slots:
 void add_clicked();
}

void MainWindow::add_clicked()
{
    QFileDialog dlg(this, Qt::Sheet);
    QString filename = dlg.getOpenFileName(this);

    if(!filename.isEmpty())
    {
        QStringList path = filename.split(QDir::separator());
        QString file = path.at(path.count()); // Index out of range assertion.

        if(!lst_tables->openDatabase(filename))
        {
            [...]
        }
    }
}

I want this error to be caught as an unhandled exception AND the application to quit without showing the user the default crash window on Windows/Mac operating system. I just want it to quit nicely after writing the assertion message to a file, etc.

解决方案

Override QCoreApplication::notify() and add try-catch there. That, and something in main() covers most cases in my experience.

Here's sort-of how I do it. Note that I'm using C++ RTTI here, not Qt's version, but that's just for convenience in our apps. Also, we put up a QMessageBox with the info and a link to our log-file. You should expand according to your own needs.

bool QMyApplication::notify(QObject* receiver, QEvent* even)
{
    try {
        return QApplication::notify(receiver, event);
    } catch (std::exception &e) {
        qFatal("Error %s sending event %s to object %s (%s)", 
            e.what(), typeid(*event).name(), qPrintable(receiver->objectName()),
            typeid(*receiver).name());
    } catch (...) {
        qFatal("Error <unknown> sending event %s to object %s (%s)", 
            typeid(*event).name(), qPrintable(receiver->objectName()),
            typeid(*receiver).name());
    }        

    // qFatal aborts, so this isn't really necessary
    // but you might continue if you use a different logging lib
    return false;
}

In addition we use the __try, __except on Windows to catch asyncronous exceptions (access violations). Google Breakpad could probably serve as a cross-platform substitute for that.

这篇关于Qt / C ++错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆