Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
src_corelib_thread_qexception.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QException>
5#include <QtConcurrent>
6
8{
9 //! [0]
10 class MyException : public QException
11 {
12 public:
13 void raise() const override { throw *this; }
14 MyException *clone() const override { return new MyException(*this); }
15 };
16 //! [0]
17}
18
19class MyException : public QException
20{
21public:
22 void raise() const override;
24};
25
26void throwFunction(int num)
27{
28 throw MyException();
29}
30
31void example(QList <int> &list)
32{
33 //! [1]
34 try {
35 QtConcurrent::blockingMap(list, throwFunction); // throwFunction throws MyException
36 } catch (MyException &e) {
37 // handle exception
38 }
39 //! [1]
40}
41
42//! [2]
43void MyException::raise() const { throw *this; }
44//! [2]
45
46
47//! [3]
48MyException *MyException::clone() const { return new MyException(*this); }
49//! [3]
50
52{
53 //! [4]
54 try {
55 auto f = QtConcurrent::run([] { throw MyException {}; });
56 // ...
57 } catch (const QUnhandledException &e) {
58 try {
59 if (e.exception())
60 std::rethrow_exception(e.exception());
61 } catch (const MyException &ex) {
62 // Process 'ex'
63 }
64 }
65 //! [4]
66}
void raise() const override
In your QException subclass, reimplement raise() like this:
MyException * clone() const override
In your QException subclass, reimplement clone() like this:
void raise() const override
[2]
MyException * clone() const override
[2]
void wrapInFunction()
[16]
void example(QList< int > &list)
void throwFunction(int num)