<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 820: current_exception()'s interaction with throwing copy ctors</title>
<meta property="og:title" content="Issue 820: current_exception()'s interaction with throwing copy ctors">
<meta property="og:description" content="C++ library issue. Status: CD1">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue820.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#CD1">CD1</a> status.</em></p>
<h3 id="820"><a href="lwg-defects.html#820">820</a>. <code>current_exception()</code>'s interaction with throwing copy ctors</h3>
<p><b>Section:</b> 17.9.7 <a href="https://wg21.link/propagation">[propagation]</a> <b>Status:</b> <a href="lwg-active.html#CD1">CD1</a>
 <b>Submitter:</b> Stephan T. Lavavej <b>Opened:</b> 2008-03-26 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#propagation">issues</a> in [propagation].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#CD1">CD1</a> status.</p>
<p><b>Discussion:</b></p>
<p>
As of N2521, the Working Paper appears to be silent about what
<code>current_exception()</code> should do if it tries to copy the currently handled
exception and its copy constructor throws.  17.9.7 <a href="https://wg21.link/propagation">[propagation]</a>/7 says "If the
function needs to allocate memory and the attempt fails, it returns an
<code>exception_ptr</code> object that refers to an instance of <code>bad_alloc</code>.", but
doesn't say anything about what should happen if memory allocation
succeeds but the actual copying fails.
</p>

<p>
I see three alternatives: (1) return an <code>exception_ptr</code> object that refers
to an instance of some fixed exception type, (2) return an <code>exception_ptr</code>
object that refers to an instance of the copy ctor's thrown exception
(but if that has a throwing copy ctor, an infinite loop can occur), or
(3) call <code>terminate()</code>.
</p>

<p>
I believe that <code>terminate()</code> is the most reasonable course of action, but
before we go implement that, I wanted to raise this issue.
</p>

<p><i>[
Peter's summary:
]</i></p>


<blockquote>
<p>
The current practice is to not have throwing copy constructors in
exception classes, because this can lead to <code>terminate()</code> as described in
14.6.2 <a href="https://wg21.link/except.terminate">[except.terminate]</a>. Thus calling <code>terminate()</code> in this situation seems
consistent and does not introduce any new problems.
</p>

<p>
However, the resolution of core issue 475 may relax this requirement:
</p>

<blockquote><p>
The CWG agreed with the position that <code>std::uncaught_exception()</code> should
return <code>false</code> during the copy to the exception object and that <code>std::terminate()</code>
should not be called if that constructor exits with an exception.
</p></blockquote>

<p>
Since throwing copy constructors will no longer call <code>terminate()</code>, option
(3) doesn't seem reasonable as it is deemed too drastic a response in a
recoverable situation.
</p>

<p>
Option (2) cannot be adopted by itself, because a potential infinite
recursion will need to be terminated by one of the other options.
</p>

</blockquote>


<p id="res-820"><b>Proposed resolution:</b></p>
<p>
Add the following paragraph after 17.9.7 <a href="https://wg21.link/propagation">[propagation]</a>/7:
</p>

<blockquote>
<p>
<i>Returns (continued):</i> If the attempt to copy the current exception
object throws an exception, the function returns an <code>exception_ptr</code> that
refers to the thrown exception or, if this is not possible, to an
instance of <code>bad_exception</code>.
</p>
<p>
[<i>Note:</i> The copy constructor of the thrown exception may also fail, so
the implementation is allowed to substitute a <code>bad_exception</code> to avoid
infinite recursion. <i>-- end note.</i>]
</p>
</blockquote>



<p><b>Rationale:</b></p>
<p><i>[
San Francisco:
]</i></p>


<blockquote>
<p>
Pete: there may be an implied assumption in the proposed wording that
current_exception() copies the existing exception object; the
implementation may not actually do that.
</p>
<p>
Pete will make the required editorial tweaks to rectify this.
</p>
</blockquote>





</body>
</html>
