Qt/C++事件循环异常处理

发布于 2024-08-30 18:38:30 字数 214 浏览 1 评论 0原文

我有一个严重基于 QT 和许多第三方库的应用程序。在某些情况下,这些会抛出一些异常。

在本机 Qt 应用程序中,这会导致应用程序中止或终止。通常,主数据模型仍然完好无损,因为我将其保存在纯 Qt 中,没有外部数据。

因此,我想我也可以通过告诉用户在这个过程中发生了错误,他应该立即保存,甚至决定继续处理主模型来恢复。

目前该程序只是默默地退出,甚至没有讲述一个故事。

I am having an application heavily based on QT and on a lot of third party libs. These happen to throw some exceptions in several cases.

In a native Qt App this causes the application to abort or terminate. Often the main data model is still intact as I am keeping it in pure Qt with no external data.

So I am thinking that I could also just recover by telling the user that there has occurred an error in this an that process and he should save now or even decide to continue working on the main model.

Currently the program just silently exits without even telling a story.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

比忠 2024-09-06 18:38:30

有时捕获所有异常确实很难。如果不小心漏掉了一个异常,以下内容会很有帮助。继承QApplication并按以下方式重写notify()函数

bool MyApplication::notify( 
QObject * receiver, 
QEvent *  event ) 
{
    try 
    {
        return QApplication::notify(receiver, event);
    }
    catch(...)
    {
        assert( !"Oops. Forgot to catch exception?" );

        // may be handle exception here ...
    }

    return false;
}

然后替换main()中的QApplication > 通过您的自定义类实现功能。所有的事件都是通过这个函数发出的,这样所有的异常都可以被捕获,你的应用程序变得稳定。

Sometimes it's really hard to catch all exception. If one exception accidently slips through, the following helps a lot. Inherit from QApplication and override the notify() function in the following way

bool MyApplication::notify( 
QObject * receiver, 
QEvent *  event ) 
{
    try 
    {
        return QApplication::notify(receiver, event);
    }
    catch(...)
    {
        assert( !"Oops. Forgot to catch exception?" );

        // may be handle exception here ...
    }

    return false;
}

Then replace the QApplication in your main() function by your custom class. All events and slots are issued through this function, so that all exceptions can be caught and your application becomes stable.

旧梦荧光笔 2024-09-06 18:38:30

正如 Qt 文档此处所述,Qt 目前并不完全异常安全。该页面上的“从异常中恢复”部分描述了抛出异常时在 Qt 应用程序中可以执行的唯一操作 - 清理并退出应用程序。

鉴于您使用的第三方库确实会引发异常,因此您需要在外部库和 Qt 代码之间的边界捕获这些异常,并在那里处理它们 - 正如 Caleb 的评论中所述。如果必须将错误传播到 Qt 应用程序中,则必须通过返回错误代码(如果可能)或发布事件来完成。

As stated in the Qt documentation here, Qt is currently not fully exception safe. The "Recovering from exceptions" section on that page describes the only thing which you can do in a Qt application when an exception is thrown - clean up and exit the app.

Given that you are using third party libraries which do throw exceptions, you need to catch these at the boundary between the external library and the Qt code, and handle them there - as stated in Caleb's comment. If the error must be propagated into the Qt application, this must be done either by returning an error code (if possible), or by posting an event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文