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