QException Class

QException 类为可跨线程传输的异常提供了一个基类。更多

头文件: #include <QException>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
继承于

QUnhandledException

公共函数

QException()
QException(const QException &other)
virtual ~QException()
virtual QException *clone() const
virtual void raise() const
QException &operator=(const QException &other)

详细说明

Qt Concurrent QException 支持跨线程抛出和捕获异常,前提是异常继承自 QException 并实现两个辅助函数:

class MyException : public QException
{
public:
    void raise() const override { throw *this; }
    MyException *clone() const override { return new MyException(*this); }
};

QException 子类必须通过值抛出,通过引用捕获:

try  {
    QtConcurrent::blockingMap(list, throwFunction); // throwFunction throws MyException
} catch (MyException &e) {
    // handle exception
}

如果你抛出的异常不是 QException 的子类,那么在接收器中的 Qt Concurrent函数将在接收线程中抛出QUnhandledException

使用QFuture 时,调用以下函数时将抛出转移异常:

成员函数文档

[noexcept] QException::QException()

构造一个 QException 对象。

[constexpr noexcept] QException::QException(const QException &other)

创建other 的副本。

注意: 使用此函数时请务必小心,否则有可能造成切片。

另请参阅 clone() 。

[virtual noexcept] QException::~QException()

销毁QException 对象。

[virtual] QException *QException::clone() const

QException 子类中,像这样重新实现 clone():

MyException *MyException::clone() const { return new MyException(*this); }

[virtual] void QException::raise() const

QException 子类中,像这样重新实现 raise():

void MyException::raise() const { throw *this; }

[noexcept] QException &QException::operator=(const QException &other)

复制--在此对象上分配other

注意: 使用此功能时要小心,因为有可能造成切片。

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.