<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- qexception.cpp --> <title>QException Class | Qt Core 5.9</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.9</td><td ><a href="qtcore-index.html">Qt Core</a></td><td ><a href="qtcore-module.html">C++ Classes</a></td><td >QException</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right">Qt 5.9.4 Reference Documentation</td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#public-functions">Public Functions</a></li> <li class="level1"><a href="#details">Detailed Description</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">QException Class</h1> <!-- $$$QException-brief --> <p>The <a href="qexception.html">QException</a> class provides a base class for exceptions that can transferred across threads. <a href="#details">More...</a></p> <!-- @@@QException --> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft rightAlign topAlign"> Header:</td><td class="memItemRight bottomAlign"> <span class="preprocessor">#include <QException></span> </td></tr><tr><td class="memItemLeft rightAlign topAlign"> qmake:</td><td class="memItemRight bottomAlign"> QT += core</td></tr><tr><td class="memItemLeft rightAlign topAlign"> Since:</td><td class="memItemRight bottomAlign"> Qt 5.0</td></tr><tr><td class="memItemLeft rightAlign topAlign"> Inherited By:</td><td class="memItemRight bottomAlign"> <p><a href="qunhandledexception.html">QUnhandledException</a></p> </td></tr></table></div><ul> <li><a href="qexception-members.html">List of all members, including inherited members</a></li> </ul> <a name="public-functions"></a> <h2 id="public-functions">Public Functions</h2> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft rightAlign topAlign"> virtual QException *</td><td class="memItemRight bottomAlign"><b><a href="qexception.html#clone">clone</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> virtual void </td><td class="memItemRight bottomAlign"><b><a href="qexception.html#raise">raise</a></b>() const</td></tr> </table></div> <a name="details"></a> <!-- $$$QException-description --> <div class="descr"> <h2 id="details">Detailed Description</h2> <p>The <a href="qexception.html">QException</a> class provides a base class for exceptions that can transferred across threads.</p> <p>Qt Concurrent supports throwing and catching exceptions across thread boundaries, provided that the exception inherit from <a href="qexception.html">QException</a> and implement two helper functions:</p> <pre class="cpp"> <span class="keyword">class</span> MyException : <span class="keyword">public</span> <span class="type"><a href="qexception.html">QException</a></span> { <span class="keyword">public</span>: <span class="type">void</span> raise() <span class="keyword">const</span> { <span class="keyword">throw</span> <span class="operator">*</span><span class="keyword">this</span>; } MyException <span class="operator">*</span>clone() <span class="keyword">const</span> { <span class="keyword">return</span> <span class="keyword">new</span> MyException(<span class="operator">*</span><span class="keyword">this</span>); } }; </pre> <p><a href="qexception.html">QException</a> subclasses must be thrown by value and caught by reference:</p> <pre class="cpp"> <span class="keyword">try</span> { <span class="type"><a href="../qtconcurrent/qtconcurrent.html">QtConcurrent</a></span><span class="operator">::</span>blockingMap(list<span class="operator">,</span> throwFunction); <span class="comment">// throwFunction throws MyException</span> } <span class="keyword">catch</span> (MyException <span class="operator">&</span>e) { <span class="comment">// handle exception</span> } </pre> <p>If you throw an exception that is not a subclass of <a href="qexception.html">QException</a>, the Qt functions will throw a <a href="qunhandledexception.html">QUnhandledException</a> in the receiver thread.</p> <p>When using <a href="qfuture.html">QFuture</a>, transferred exceptions will be thrown when calling the following functions:</p> <ul> <li><a href="qfuture.html#waitForFinished">QFuture::waitForFinished</a>()</li> <li><a href="qfuture.html#result">QFuture::result</a>()</li> <li><a href="qfuture.html#resultAt">QFuture::resultAt</a>()</li> <li><a href="qfuture.html#results">QFuture::results</a>()</li> </ul> </div> <!-- @@@QException --> <div class="func"> <h2>Member Function Documentation</h2> <!-- $$$clone[overload1]$$$clone --> <h3 class="fn" id="clone"><a name="clone"></a><code>[virtual] </code><span class="type"><a href="qexception.html">QException</a></span> *QException::<span class="name">clone</span>() const</h3> <p>In your <a href="qexception.html">QException</a> subclass, reimplement clone() like this:</p> <pre class="cpp"> MyException <span class="operator">*</span>MyException<span class="operator">::</span>clone() <span class="keyword">const</span> { <span class="keyword">return</span> <span class="keyword">new</span> MyException(<span class="operator">*</span><span class="keyword">this</span>); } </pre> <!-- @@@clone --> <!-- $$$raise[overload1]$$$raise --> <h3 class="fn" id="raise"><a name="raise"></a><code>[virtual] </code><span class="type">void</span> QException::<span class="name">raise</span>() const</h3> <p>In your <a href="qexception.html">QException</a> subclass, reimplement raise() like this:</p> <pre class="cpp"> <span class="type">void</span> MyException<span class="operator">::</span>raise() <span class="keyword">const</span> { <span class="keyword">throw</span> <span class="operator">*</span><span class="keyword">this</span>; } </pre> <!-- @@@raise --> </div> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2017 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>