1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qexception.h" |
5 | #include "QtCore/qshareddata.h" |
6 | |
7 | #if !defined(QT_NO_EXCEPTIONS) || defined(Q_QDOC) |
8 | |
9 | QT_BEGIN_NAMESPACE |
10 | |
11 | /*! |
12 | \class QException |
13 | \inmodule QtCore |
14 | \brief The QException class provides a base class for exceptions that can be transferred across threads. |
15 | \since 5.0 |
16 | |
17 | Qt Concurrent supports throwing and catching exceptions across thread |
18 | boundaries, provided that the exception inherits from QException |
19 | and implements two helper functions: |
20 | |
21 | \snippet code/src_corelib_thread_qexception.cpp 0 |
22 | |
23 | QException subclasses must be thrown by value and |
24 | caught by reference: |
25 | |
26 | \snippet code/src_corelib_thread_qexception.cpp 1 |
27 | |
28 | If you throw an exception that is not a subclass of QException, |
29 | the \l{Qt Concurrent} functions will throw a QUnhandledException |
30 | in the receiver thread. |
31 | |
32 | When using QFuture, transferred exceptions will be thrown when calling the following functions: |
33 | \list |
34 | \li QFuture::waitForFinished() |
35 | \li QFuture::result() |
36 | \li QFuture::resultAt() |
37 | \li QFuture::results() |
38 | \endlist |
39 | */ |
40 | |
41 | /*! |
42 | \fn QException::raise() const |
43 | In your QException subclass, reimplement raise() like this: |
44 | |
45 | \snippet code/src_corelib_thread_qexception.cpp 2 |
46 | */ |
47 | |
48 | /*! |
49 | \fn QException::clone() const |
50 | In your QException subclass, reimplement clone() like this: |
51 | |
52 | \snippet code/src_corelib_thread_qexception.cpp 3 |
53 | */ |
54 | |
55 | /*! |
56 | \class QUnhandledException |
57 | \inmodule QtCore |
58 | |
59 | \brief The QUnhandledException class represents an unhandled exception in a |
60 | Qt Concurrent worker thread. |
61 | \since 5.0 |
62 | |
63 | If a worker thread throws an exception that is not a subclass of QException, |
64 | the \l{Qt Concurrent} functions will throw a QUnhandledException on the receiver |
65 | thread side. The information about the actual exception that has been thrown |
66 | will be saved in the QUnhandledException class and can be obtained using the |
67 | exception() method. For example, you can process the exception held by |
68 | QUnhandledException in the following way: |
69 | |
70 | \snippet code/src_corelib_thread_qexception.cpp 4 |
71 | |
72 | Inheriting from this class is not supported. |
73 | */ |
74 | |
75 | /*! |
76 | \fn QUnhandledException::raise() const |
77 | \internal |
78 | */ |
79 | |
80 | /*! |
81 | \fn QUnhandledException::clone() const |
82 | \internal |
83 | */ |
84 | |
85 | /*! |
86 | Destroys this QException object. |
87 | */ |
88 | QException::~QException() noexcept |
89 | { |
90 | } |
91 | |
92 | /*! |
93 | \fn QException::QException() |
94 | |
95 | Constructs a QException object. |
96 | */ |
97 | |
98 | /*! |
99 | \fn QException::QException(const QException &other) |
100 | |
101 | Creates a copy of \a other. |
102 | |
103 | \note Be careful when using this function, as you risk slicing. |
104 | |
105 | \sa clone() |
106 | */ |
107 | |
108 | /*! |
109 | \fn QException &QException::operator=(const QException &other) |
110 | |
111 | Copy-assigns \a other over this object. |
112 | |
113 | \note Be careful when using this function, as you risk slicing. |
114 | */ |
115 | |
116 | void QException::raise() const |
117 | { |
118 | QException e = *this; |
119 | throw e; |
120 | } |
121 | |
122 | QException *QException::clone() const |
123 | { |
124 | return new QException(*this); |
125 | } |
126 | |
127 | class QUnhandledExceptionPrivate : public QSharedData |
128 | { |
129 | public: |
130 | QUnhandledExceptionPrivate(std::exception_ptr exception) noexcept : exceptionPtr(exception) { } |
131 | std::exception_ptr exceptionPtr; |
132 | }; |
133 | |
134 | /*! |
135 | \fn QUnhandledException::QUnhandledException(std::exception_ptr exception = nullptr) noexcept |
136 | \since 6.0 |
137 | |
138 | Constructs a new QUnhandledException object. Saves the pointer to the actual |
139 | exception object if \a exception is passed. |
140 | |
141 | \sa exception() |
142 | */ |
143 | QUnhandledException::QUnhandledException(std::exception_ptr exception) noexcept |
144 | : d(new QUnhandledExceptionPrivate(exception)) |
145 | { |
146 | } |
147 | |
148 | /*! |
149 | Move-constructs a QUnhandledException, making it point to the same |
150 | object as \a other was pointing to. |
151 | */ |
152 | QUnhandledException::QUnhandledException(QUnhandledException &&other) noexcept |
153 | : d(std::exchange(obj&: other.d, new_val: {})) |
154 | { |
155 | } |
156 | |
157 | /*! |
158 | Constructs a QUnhandledException object as a copy of \a other. |
159 | */ |
160 | QUnhandledException::QUnhandledException(const QUnhandledException &other) noexcept |
161 | : d(other.d) |
162 | { |
163 | } |
164 | |
165 | /*! |
166 | Assigns \a other to this QUnhandledException object and returns a reference |
167 | to this QUnhandledException object. |
168 | */ |
169 | QUnhandledException &QUnhandledException::operator=(const QUnhandledException &other) noexcept |
170 | { |
171 | d = other.d; |
172 | return *this; |
173 | } |
174 | |
175 | /*! |
176 | \fn void QUnhandledException::swap(QUnhandledException &other) |
177 | \since 6.0 |
178 | \memberswap{unhandled exception object} |
179 | */ |
180 | |
181 | /*! |
182 | \since 6.0 |
183 | |
184 | Returns a \l{https://en.cppreference.com/w/cpp/error/exception_ptr}{pointer} to |
185 | the actual exception that has been saved in this QUnhandledException. Returns a |
186 | \c null pointer, if it does not point to an exception object. |
187 | */ |
188 | std::exception_ptr QUnhandledException::exception() const |
189 | { |
190 | return d->exceptionPtr; |
191 | } |
192 | |
193 | QUnhandledException::~QUnhandledException() noexcept |
194 | { |
195 | } |
196 | |
197 | void QUnhandledException::raise() const |
198 | { |
199 | QUnhandledException e = *this; |
200 | throw e; |
201 | } |
202 | |
203 | QUnhandledException *QUnhandledException::clone() const |
204 | { |
205 | return new QUnhandledException(*this); |
206 | } |
207 | |
208 | #if !defined(Q_QDOC) |
209 | |
210 | namespace QtPrivate { |
211 | |
212 | void ExceptionStore::setException(const QException &e) |
213 | { |
214 | Q_ASSERT(!hasException()); |
215 | try { |
216 | e.raise(); |
217 | } catch (...) { |
218 | exceptionHolder = std::current_exception(); |
219 | } |
220 | } |
221 | |
222 | void ExceptionStore::setException(std::exception_ptr e) |
223 | { |
224 | Q_ASSERT(!hasException()); |
225 | exceptionHolder = e; |
226 | } |
227 | |
228 | bool ExceptionStore::hasException() const |
229 | { |
230 | return !!exceptionHolder; |
231 | } |
232 | |
233 | std::exception_ptr ExceptionStore::exception() const |
234 | { |
235 | return exceptionHolder; |
236 | } |
237 | |
238 | void ExceptionStore::throwPossibleException() |
239 | { |
240 | if (hasException()) |
241 | std::rethrow_exception(exceptionHolder); |
242 | } |
243 | |
244 | void ExceptionStore::rethrowException() const |
245 | { |
246 | Q_ASSERT(hasException()); |
247 | std::rethrow_exception(exceptionHolder); |
248 | } |
249 | |
250 | } // namespace QtPrivate |
251 | |
252 | #endif //Q_QDOC |
253 | |
254 | QT_END_NAMESPACE |
255 | |
256 | #endif // QT_NO_EXCEPTIONS |
257 | |