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

查看:28
本文介绍了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++ 时遇到的问题是,必须在处理之前考虑任何已完成的错误处理(想想 try/catch 或大量条件).根据我的经验,代码中的问题是事先没有想到的,否则一开始就不会有问题.

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++ 特定的全能"错误捕获机制可以在我的应用程序中使用,这样,如果出现问题,我至少可以在它之前写一份报告崩溃?

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

此外,我们在 Windows 上使用 __try、__except 来捕获异步异常(访问冲突).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天全站免登陆