1. <small id='RZTEq'></small><noframes id='RZTEq'>

      1. <tfoot id='RZTEq'></tfoot>

        <i id='RZTEq'><tr id='RZTEq'><dt id='RZTEq'><q id='RZTEq'><span id='RZTEq'><b id='RZTEq'><form id='RZTEq'><ins id='RZTEq'></ins><ul id='RZTEq'></ul><sub id='RZTEq'></sub></form><legend id='RZTEq'></legend><bdo id='RZTEq'><pre id='RZTEq'><center id='RZTEq'></center></pre></bdo></b><th id='RZTEq'></th></span></q></dt></tr></i><div id='RZTEq'><tfoot id='RZTEq'></tfoot><dl id='RZTEq'><fieldset id='RZTEq'></fieldset></dl></div>
        <legend id='RZTEq'><style id='RZTEq'><dir id='RZTEq'><q id='RZTEq'></q></dir></style></legend>
          <bdo id='RZTEq'></bdo><ul id='RZTEq'></ul>
      2. Qt/C++ 错误处理

        时间:2023-08-28

            <i id='W2xCw'><tr id='W2xCw'><dt id='W2xCw'><q id='W2xCw'><span id='W2xCw'><b id='W2xCw'><form id='W2xCw'><ins id='W2xCw'></ins><ul id='W2xCw'></ul><sub id='W2xCw'></sub></form><legend id='W2xCw'></legend><bdo id='W2xCw'><pre id='W2xCw'><center id='W2xCw'></center></pre></bdo></b><th id='W2xCw'></th></span></q></dt></tr></i><div id='W2xCw'><tfoot id='W2xCw'></tfoot><dl id='W2xCw'><fieldset id='W2xCw'></fieldset></dl></div>
            <tfoot id='W2xCw'></tfoot>

            <small id='W2xCw'></small><noframes id='W2xCw'>

              <bdo id='W2xCw'></bdo><ul id='W2xCw'></ul>
            • <legend id='W2xCw'><style id='W2xCw'><dir id='W2xCw'><q id='W2xCw'></q></dir></style></legend>
                  <tbody id='W2xCw'></tbody>
                • 本文介绍了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++ 错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用了未初始化的局部变量“j" 下一篇:C++ 社区是否就何时应该使用异常达成普遍共识?

                  相关文章

                  • <bdo id='GtJB9'></bdo><ul id='GtJB9'></ul>

                  1. <i id='GtJB9'><tr id='GtJB9'><dt id='GtJB9'><q id='GtJB9'><span id='GtJB9'><b id='GtJB9'><form id='GtJB9'><ins id='GtJB9'></ins><ul id='GtJB9'></ul><sub id='GtJB9'></sub></form><legend id='GtJB9'></legend><bdo id='GtJB9'><pre id='GtJB9'><center id='GtJB9'></center></pre></bdo></b><th id='GtJB9'></th></span></q></dt></tr></i><div id='GtJB9'><tfoot id='GtJB9'></tfoot><dl id='GtJB9'><fieldset id='GtJB9'></fieldset></dl></div>
                  2. <legend id='GtJB9'><style id='GtJB9'><dir id='GtJB9'><q id='GtJB9'></q></dir></style></legend>
                  3. <small id='GtJB9'></small><noframes id='GtJB9'>

                      <tfoot id='GtJB9'></tfoot>