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

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

问题描述

我一直在做很多关于使用Qt / C ++处理错误的研究,我仍然像我开始时一样迷失。也许我正在寻找一个简单的出路(像其他语言提供)。一个,特别是,提供了一个未经处理的异常,我用宗教。当程序遇到问题时,它抛出未处理的异常,以便我可以创建自己的错误报告。

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.

我使用C ++的问题是,任何错误处理完成后,被认为是BEFORE手(想尝试/捕捉或大规模条件)。在我的经验中,代码中的问题不是以前的想法,否则将不会有一个问题开始。

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.

我的问题是:是否有任何种类的Qt或C ++特定的catch-all错误陷阱机制,我可以在我的应用程序中使用,这样,如果有什么问题,我至少可以在崩溃之前写一个报告?

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?

示例:


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))
        {
            [...]
        }
    }
}

我想要将此错误作为未处理的异常捕获,应用程序退出时不会向用户显示默认崩溃窗口在Windows / Mac操作系统。

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.

推荐答案

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

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

这里是我做的。注意,我在这里使用C + + RTTI,而不是Qt的版本,但这只是为了方便在我们的应用程序。此外,我们建立了一个QMessageBox与信息和链接到我们的日志文件。

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;
}

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

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天全站免登陆