Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>throw()</code> (or <code>noexcept</code> in C++11) is useful for two reasons:</p> <ol> <li>It allows the compiler to be more aggressive in its optimisations.</li> <li>It tells function users that they can use this function in their own non-throwing functions.</li> </ol> <p>Non-throwing functions are very important for writing exception safe code. For instance, the usual way of writing an exception safe <code>operator=</code> is via a non-throwing <code>swap()</code> function.</p> <p>On the other hand, other exception specifications are useless and have been justly deprecated in the current standard. They don't mix well at all with templates and are too expensive to enforce.</p> <p>Now, if you use a <code>noexcept</code> specification in a function that might actually throw, all bets are off. Even if your compiler does not terminate the program when an exception leaves the function (VS does not do it for runtime efficiency reasons, for instance), your code might not do what you thought because of optimisations. For example:</p> <pre><code>void f() noexcept { a(); b(); } </code></pre> <p>If <code>a()</code> actually throws and <code>b()</code> has side effects, the function behaviour will be unpredictable, because your compiler may decide to execute <code>b()</code> before <code>a()</code>, since you have told it that no exceptions will be thrown.</p> <p>EDIT: Now for the second part of your question: how to make a function non-throwing?</p> <p>First you need to ask yourself whether your function should really be non-throwing. For instance:</p> <pre><code>class C { C* CreateInstance() { return new C(); } } </code></pre> <p>Since operator <code>new</code> can throw a <code>std::bad_alloc</code>, <code>CreateInstance()</code> can throw. You could try to avoid it with a try-catch block, handling or swallowing any exceptions that may be thrown inside the <code>try</code> block, but would that really be sensible? For instance:</p> <pre><code>C* CreateInstance() { try { return new C(); } catch (...) { return null; } } </code></pre> <p>Problem solved, it seems, but would your callers be prepared for <code>CreateInstance()</code> returning <code>null</code>? If not, a crash will ensue when they try to use that pointer. Besides, a <code>std::bad_alloc</code> usually means that you have run out of memory and you would just be postponing the problem.</p> <p>So be careful: some functions can be made non-throwing, but others should be allowed to throw. Exception safety is not a trivial matter.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload