抛出Qt QException子类,但捕获了QUnhandledException

时间:2016-12-02 11:48:00

标签: c++ qt exception

我创建了一个QException的子类,如下所示:

#include <QtConcurrent>
#include <QException>

class ProcessingException : public QException
{
public:
    ProcessingException(QString const& message) :
        message(message)
    {}

    virtual ~ProcessingException()
    {

    }

    void raise() const { throw *this; }
    ProcessingException *clone() const { return new ProcessingException(*this); }

    QString getMessage() const
    {
        return message;
    }
private:
    QString message;
};

从QtConcurrent :: run运行的方法中,我使用throw new ProcessingException("test");抛出此子类的实例。然后,如果我正确理解文档,当我使用.waitForFinished()收集未来的结果时,Qt应该重新抛出QException子类。但是,使用这种方法我只能捕获QUnhandledException:

try
{
    watcher->waitForFinished();
}
catch(const ProcessingException &e)
{
    qCritical() << "Caught:" << e.getMessage();
}
catch(const QUnhandledException &e)
{
    qCritical() << "Uncaught qexception!";
}

我错过了什么?

1 个答案:

答案 0 :(得分:3)

你正在抛出一个指针,但却抓住了一个参考....

只需使用throw ProcessingException("test");即可。 您可以通过const引用来捕获它,并且在捕获它之后不必清理(调用delete)。

相关:throw new std::exception vs throw std::exception

相关问题