From 4c793e6353ece51d4c04373f54e13d540b45195e Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Fri, 16 Oct 2020 12:25:13 +0200 Subject: Store std::exception_ptr in QUnhandledException For historical reasons Qt Concurrent reports QUnhandledException in case if an exception that is not derived from QException is thrown from a worker thread. Changing this behavior may not be a good idea, since the existing user code may rely on it. Changed QUnhandledException to wrap the std::exception_ptr to the actual exception, so that the users can obtain the information about the thrown exception if needed. [ChangeLog][QtCore][QUnhandledException] Improved QUnhandledException to store the std::exception_ptr to the actual exception thrown from a QtCocnurrent worker thread. Change-Id: I30e7c1d3e01aff6e1ed9938c421da0a888f12066 Reviewed-by: Fabian Kosmale --- src/corelib/thread/qexception.cpp | 82 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 4 deletions(-) (limited to 'src/corelib/thread/qexception.cpp') diff --git a/src/corelib/thread/qexception.cpp b/src/corelib/thread/qexception.cpp index 1a6ee7bd019..c9b7fb6cf60 100644 --- a/src/corelib/thread/qexception.cpp +++ b/src/corelib/thread/qexception.cpp @@ -62,7 +62,7 @@ QT_BEGIN_NAMESPACE \snippet code/src_corelib_thread_qexception.cpp 1 If you throw an exception that is not a subclass of QException, - the Qt functions will throw a QUnhandledException + the \l{Qt Concurrent} functions will throw a QUnhandledException in the receiver thread. When using QFuture, transferred exceptions will be thrown when calling the following functions: @@ -92,12 +92,18 @@ QT_BEGIN_NAMESPACE \class QUnhandledException \inmodule QtCore - \brief The UnhandledException class represents an unhandled exception in a worker thread. + \brief The QUnhandledException class represents an unhandled exception in a + Qt Concurrent worker thread. \since 5.0 If a worker thread throws an exception that is not a subclass of QException, - the Qt functions will throw a QUnhandledException - on the receiver thread side. + the \l{Qt Concurrent} functions will throw a QUnhandledException on the receiver + thread side. The information about the actual exception that has been thrown + will be saved in the QUnhandledException class and can be obtained using the + exception() method. For example, you can process the exception held by + QUnhandledException in the following way: + + \snippet code/src_corelib_thread_qexception.cpp 4 Inheriting from this class is not supported. */ @@ -127,6 +133,74 @@ QException *QException::clone() const return new QException(*this); } +class QUnhandledExceptionPrivate : public QSharedData +{ +public: + QUnhandledExceptionPrivate(std::exception_ptr exception) noexcept : exceptionPtr(exception) { } + std::exception_ptr exceptionPtr; +}; + +/*! + \fn QUnhandledException::QUnhandledException(std::exception_ptr exception = nullptr) noexcept + \since 6.0 + + Constructs a new QUnhandledException object. Saves the pointer to the actual + exception object if \a exception is passed. + + \sa exception() +*/ +QUnhandledException::QUnhandledException(std::exception_ptr exception) noexcept + : d(new QUnhandledExceptionPrivate(exception)) +{ +} + +/*! + Move-constructs a QUnhandledException, making it point to the same + object as \a other was pointing to. +*/ +QUnhandledException::QUnhandledException(QUnhandledException &&other) noexcept + : d(std::exchange(other.d, {})) +{ +} + +/*! + Constructs a QUnhandledException object as a copy of \a other. +*/ +QUnhandledException::QUnhandledException(const QUnhandledException &other) noexcept + : d(other.d) +{ +} + +/*! + Assigns \a other to this QUnhandledException object and returns a reference + to this QUnhandledException object. +*/ +QUnhandledException &QUnhandledException::operator=(const QUnhandledException &other) noexcept +{ + d = other.d; + return *this; +} + +/*! + \fn void QUnhandledException::swap(QUnhandledException &other) + \since 6.0 + + Swaps this QUnhandledException with \a other. This function is very fast and + never fails. +*/ + +/*! + \since 6.0 + + Returns a \l{https://en.cppreference.com/w/cpp/error/exception_ptr}{pointer} to + the actual exception that has been saved in this QUnhandledException. Returns a + \c null pointer, if it does not point to an exception object. +*/ +std::exception_ptr QUnhandledException::exception() const +{ + return d->exceptionPtr; +} + QUnhandledException::~QUnhandledException() noexcept { } -- cgit v1.2.3