More verbose description of throwing an exception from a QtConcurrent thread (#31)

This commit is contained in:
Dmitriy Purgin 2020-02-18 22:31:41 +01:00 committed by GitHub
parent be5455a8c8
commit b99e468c84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

View File

@ -206,3 +206,6 @@ download(url).then(&uncompress).then([](const Entries& entries) {
// {...} catch all // {...} catch all
}); });
``` ```
Note that `MalformedException` in the example above is thrown from a QtConcurrent thread and should
meet [specific conditions](qtconcurrent.md#error).

View File

@ -50,12 +50,27 @@ The `output` promise is resolved when the `QFuture` is [finished](https://doc.qt
## Error ## Error
Exceptions thrown from a QtConcurrent thread reject the associated promise with the exception as the Exceptions thrown from a QtConcurrent thread reject the associated promise with the exception as the
reason. Note that if you throw an exception that is not a subclass of `QException`, the promise will reason. For this to work, the exception should be a [`QException`](https://doc.qt.io/qt-5/qexception.html)
or its subclass. Correct subclassing of [`QException`](https://doc.qt.io/qt-5/qexception.html)
includes overriding its methods [`clone()`](https://doc.qt.io/qt-5/qexception.html#clone) and
[`raise()`](https://doc.qt.io/qt-5/qexception.html#raise). Without these overrides the promise will
be rejected with [`QException`](https://doc.qt.io/qt-5/qexception.html).
Note that if you throw an exception that is not a subclass of `QException`, the promise will
be rejected with [`QUnhandledException`](https://doc.qt.io/qt-5/qunhandledexception.html#details) be rejected with [`QUnhandledException`](https://doc.qt.io/qt-5/qunhandledexception.html#details)
(this restriction only applies to exceptions thrown from a QtConcurrent thread, (this restriction only applies to exceptions thrown from a QtConcurrent thread,
[read more](https://doc.qt.io/qt-5/qexception.html#details)). [read more](https://doc.qt.io/qt-5/qexception.html#details)).
```cpp ```cpp
class CustomException : public QException
{
public:
void raise() const override { throw *this; }
CustomException* clone() const override { return new CustomException{*this}; }
};
// {...}
QPromise<int> promise = ... QPromise<int> promise = ...
promise.then([](int res) { promise.then([](int res) {
return QtConcurrent::run([]() { return QtConcurrent::run([]() {