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 | QException::~QException() noexcept |
86 | { |
87 | } |
88 | |
89 | void QException::raise() const |
90 | { |
91 | QException e = *this; |
92 | throw e; |
93 | } |
94 | |
95 | QException *QException::clone() const |
96 | { |
97 | return new QException(*this); |
98 | } |
99 | |
100 | class QUnhandledExceptionPrivate : public QSharedData |
101 | { |
102 | public: |
103 | QUnhandledExceptionPrivate(std::exception_ptr exception) noexcept : exceptionPtr(exception) { } |
104 | std::exception_ptr exceptionPtr; |
105 | }; |
106 | |
107 | /*! |
108 | \fn QUnhandledException::QUnhandledException(std::exception_ptr exception = nullptr) noexcept |
109 | \since 6.0 |
110 | |
111 | Constructs a new QUnhandledException object. Saves the pointer to the actual |
112 | exception object if \a exception is passed. |
113 | |
114 | \sa exception() |
115 | */ |
116 | QUnhandledException::QUnhandledException(std::exception_ptr exception) noexcept |
117 | : d(new QUnhandledExceptionPrivate(exception)) |
118 | { |
119 | } |
120 | |
121 | /*! |
122 | Move-constructs a QUnhandledException, making it point to the same |
123 | object as \a other was pointing to. |
124 | */ |
125 | QUnhandledException::QUnhandledException(QUnhandledException &&other) noexcept |
126 | : d(std::exchange(obj&: other.d, new_val: {})) |
127 | { |
128 | } |
129 | |
130 | /*! |
131 | Constructs a QUnhandledException object as a copy of \a other. |
132 | */ |
133 | QUnhandledException::QUnhandledException(const QUnhandledException &other) noexcept |
134 | : d(other.d) |
135 | { |
136 | } |
137 | |
138 | /*! |
139 | Assigns \a other to this QUnhandledException object and returns a reference |
140 | to this QUnhandledException object. |
141 | */ |
142 | QUnhandledException &QUnhandledException::operator=(const QUnhandledException &other) noexcept |
143 | { |
144 | d = other.d; |
145 | return *this; |
146 | } |
147 | |
148 | /*! |
149 | \fn void QUnhandledException::swap(QUnhandledException &other) |
150 | \since 6.0 |
151 | |
152 | Swaps this QUnhandledException with \a other. This function is very fast and |
153 | never fails. |
154 | */ |
155 | |
156 | /*! |
157 | \since 6.0 |
158 | |
159 | Returns a \l{https://en.cppreference.com/w/cpp/error/exception_ptr}{pointer} to |
160 | the actual exception that has been saved in this QUnhandledException. Returns a |
161 | \c null pointer, if it does not point to an exception object. |
162 | */ |
163 | std::exception_ptr QUnhandledException::exception() const |
164 | { |
165 | return d->exceptionPtr; |
166 | } |
167 | |
168 | QUnhandledException::~QUnhandledException() noexcept |
169 | { |
170 | } |
171 | |
172 | void QUnhandledException::raise() const |
173 | { |
174 | QUnhandledException e = *this; |
175 | throw e; |
176 | } |
177 | |
178 | QUnhandledException *QUnhandledException::clone() const |
179 | { |
180 | return new QUnhandledException(*this); |
181 | } |
182 | |
183 | #if !defined(Q_QDOC) |
184 | |
185 | namespace QtPrivate { |
186 | |
187 | void ExceptionStore::setException(const QException &e) |
188 | { |
189 | Q_ASSERT(!hasException()); |
190 | try { |
191 | e.raise(); |
192 | } catch (...) { |
193 | exceptionHolder = std::current_exception(); |
194 | } |
195 | } |
196 | |
197 | void ExceptionStore::setException(std::exception_ptr e) |
198 | { |
199 | Q_ASSERT(!hasException()); |
200 | exceptionHolder = e; |
201 | } |
202 | |
203 | bool ExceptionStore::hasException() const |
204 | { |
205 | return !!exceptionHolder; |
206 | } |
207 | |
208 | std::exception_ptr ExceptionStore::exception() const |
209 | { |
210 | return exceptionHolder; |
211 | } |
212 | |
213 | void ExceptionStore::throwPossibleException() |
214 | { |
215 | if (hasException()) |
216 | std::rethrow_exception(exceptionHolder); |
217 | } |
218 | |
219 | void ExceptionStore::rethrowException() const |
220 | { |
221 | Q_ASSERT(hasException()); |
222 | std::rethrow_exception(exceptionHolder); |
223 | } |
224 | |
225 | } // namespace QtPrivate |
226 | |
227 | #endif //Q_QDOC |
228 | |
229 | QT_END_NAMESPACE |
230 | |
231 | #endif // QT_NO_EXCEPTIONS |
232 | |