QException Class
QException 类为可在线程间传递的异常提供了一个基类。更多内容...
| 标题: | #include <QException> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
| 由以下接口继承: |
公共函数
| QException() | |
| QException(const QException &other) | |
| virtual | ~QException() |
| virtual QException * | clone() const |
| virtual void | raise() const |
| QException & | operator=(const QException &other) |
详细说明
Qt Concurrent 支持在跨线程边界的情况下抛出和捕获异常,前提是该异常继承自 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 default] QException::QException()
创建一个 QException 对象。
[constexpr noexcept default] 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 default] QException &QException::operator=(const QException &other)
将other 复制并赋值给该对象。
注意: 使用此函数时请务必 谨慎,因为可能会导致切片。
© 2026 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.