Move larger code examples in QFuture docs into snippets

Change-Id: I77b943124ac9c82f54b0c38e9437b9415604c21a
Reviewed-by: Karsten Heimrich <karsten.heimrich@qt.io>
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
This commit is contained in:
Sona Kurazyan 2020-04-17 12:28:21 +02:00
parent 339dd743a9
commit e6d880e511
2 changed files with 81 additions and 72 deletions

View File

@ -99,3 +99,79 @@ if (auto filePath = std::get_if<QString>(&result)) {
else else
// process the error // process the error
//! [4] //! [4]
//! [5]
QFuture<int> future = ...;
future.then([](QFuture<int> f) {
try {
...
auto result = f.result();
...
} catch (QException &e) {
// handle the exception
}
}).then(...);
//! [5]
//! [6]
QFuture<int> parentFuture = ...;
auto continuation = parentFuture.then([](int res1){ ... }).then([](int res2){ ... })...
...
// parentFuture throws an exception
try {
auto result = continuation.result();
} catch (QException &e) {
// handle the exception
}
//! [6]
//! [7]
QFuture<int> future = ...;
auto resultFuture = future.then([](int res) {
...
throw Error();
...
}).onFailed([](const Error &e) {
// Handle exceptions of type Error
...
return -1;
}).onFailed([] {
// Handle all other types of errors
...
return -1;
});
auto result = resultFuture.result(); // result is -1
//! [7]
//! [8]
QFuture<int> future = ...;
future.then([](int res) {
...
throw std::runtime_error("message");
...
}).onFailed([](const std::exception &e) {
// This handler will be invoked
}).onFailed([](const std::runtime_error &e) {
// This handler won't be invoked, because of the handler above.
});
//! [8]
//! [9]
QFuture<int> future = ...;
auto resultFuture = future.then([](int res) {
...
throw Error("message");
...
}).onFailed([](const std::exception &e) {
// Won't be invoked
}).onFailed([](const QException &e) {
// Won't be invoked
});
try {
auto result = resultFuture.result();
} catch(...) {
// Handle the exception
}
//! [9]

View File

@ -835,34 +835,13 @@
the exception of the previous future inside the continuation, to not interrupt the chain the exception of the previous future inside the continuation, to not interrupt the chain
of multiple continuations. For example: of multiple continuations. For example:
\code \snippet code/src_corelib_thread_qfuture.cpp 5
QFuture<int> future = ...;
future.then([](QFuture<int> f) {
try {
...
auto result = f.result();
...
} catch (QException &e) {
// handle the exception
}
}).then(...);
\endcode
If the previous future throws an exception and it is not handled inside the If the previous future throws an exception and it is not handled inside the
continuation, the exception will be propagated to the continuation future, to continuation, the exception will be propagated to the continuation future, to
allow the caller to handle it: allow the caller to handle it:
\code \snippet code/src_corelib_thread_qfuture.cpp 6
QFuture<int> parentFuture = ...;
auto continuation = parentFuture.then([](int res1){ ... }).then([](int res2){ ... })...
...
// parentFuture throws an exception
try {
auto result = continuation.result();
} catch (QException &e) {
// handle the exception
}
\endcode
In this case the whole chain of continuations will be interrupted. In this case the whole chain of continuations will be interrupted.
@ -931,63 +910,17 @@
For example: For example:
\code \snippet code/src_corelib_thread_qfuture.cpp 7
QFuture<int> future = ...;
auto resultFuture = future.then([](int res) {
...
throw Error();
...
}).onFailed([](const Error &e) {
// Handle exceptions of type Error
...
return -1;
}).onFailed([] {
// Handle all other types of errors
...
return -1;
});
auto result = resultFuture.result(); // result is -1
\endcode
If there are multiple handlers attached, the first handler that matches with the If there are multiple handlers attached, the first handler that matches with the
thrown exception type will be invoked. For example: thrown exception type will be invoked. For example:
\code \snippet code/src_corelib_thread_qfuture.cpp 8
QFuture<int> future = ...;
future.then([](int res) {
...
throw std::runtime_error("message");
...
}).onFailed([](const std::exception &e) {
// This handler will be invoked
}).onFailed([](const std::runtime_error &e) {
// This handler won't be invoked, because of the handler above.
});
\endcode
If none of the handlers matches with the thrown exception type, the exception If none of the handlers matches with the thrown exception type, the exception
will be propagated to the resulted future: will be propagated to the resulted future:
\code \snippet code/src_corelib_thread_qfuture.cpp 9
QFuture<int> future = ...;
auto resultFuture = future.then([](int res) {
...
throw Error("message");
...
}).onFailed([](const std::exception &e) {
// Won't be invoked
}).onFailed([](const QException &e) {
// Won't be invoked
});
try {
auto result = resultFuture.result();
} catch(...) {
// Handle the exception
}
\endcode
\note You can always attach a handler taking no argument, to handle all exception \note You can always attach a handler taking no argument, to handle all exception
types and avoid writing the try-catch block. types and avoid writing the try-catch block.