diff options
author | Sona Kurazyan <sona.kurazyan@qt.io> | 2020-04-17 12:28:21 +0200 |
---|---|---|
committer | Sona Kurazyan <sona.kurazyan@qt.io> | 2020-04-17 15:38:43 +0200 |
commit | e6d880e511db7f12bda1f8ee599246d30b7ebf1b (patch) | |
tree | 7989533f0313ff52368a1af9efa1404b59c0e7d5 /src/corelib/doc/snippets/code | |
parent | 339dd743a9e4db170f843a0ba51e5c93761f5644 (diff) |
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>
Diffstat (limited to 'src/corelib/doc/snippets/code')
-rw-r--r-- | src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp b/src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp index 4fe3f7e99e..66fa62f6b3 100644 --- a/src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp @@ -99,3 +99,79 @@ if (auto filePath = std::get_if<QString>(&result)) { else // process the error //! [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] |