<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2088: std::terminate problem</title>
<meta property="og:title" content="Issue 2088: std::terminate problem">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2088.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#Resolved">Resolved</a> status.</em></p>
<h3 id="2088"><a href="lwg-defects.html#2088">2088</a>. <code>std::terminate</code> problem</h3>
<p><b>Section:</b> 17.9.5 <a href="https://wg21.link/exception.terminate">[exception.terminate]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Daniel Kr&uuml;gler <b>Opened:</b> 2011-09-25 <b>Last modified:</b> 2024-11-07</p>
<p><b>Priority: </b>3
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>

<p>
Andrzej Krzemienski reported the following on comp.std.c++:
</p>
<blockquote>
<p>
In N3290, which is to become the official standard, in 17.9.5.4 <a href="https://wg21.link/terminate">[terminate]</a>,
paragraph 1 reads
</p>
<blockquote><p>
<i>Remarks</i>: Called by the implementation when exception handling must
be abandoned for any of several reasons (15.5.1), in effect immediately after
evaluating the <em>throw-expression</em> (18.8.3.1). May also be called directly by the
program.
</p></blockquote>
<p>It is not clear what is "in effect". It was clear in previous drafts where paragraphs
1 and 2 read:
</p>
<blockquote><p>
Called by the implementation when exception handling must be
abandoned for any of several reasons (15.5.1). May also be called directly
by the program.
<p/>
<i>Effects</i>: Calls the <code>terminate_handler</code> function in effect
immediately after evaluating the <em>throw-expression</em> (18.8.3.1), if called by the
implementation, or calls the current <code>terminate_handler</code> function,
if called by the program.
</p>
</blockquote>
<p>
It was changed by N3189. The same applies to function unexpected (D. 11.4, paragraph 1).
<p/>
Assuming the previous wording is still intended, the wording can be read
"unless <code>std::terminate</code> is called by the program, we will use the handler
that was in effect immediately after evaluating the throw-expression".
<p/>
  This assumes that there is some throw-expression connected to every
  situation that triggers the call to <code>std::terminate</code>. But this is not
  the case:
</p>
<ul>
<li>
  In case <code>std::thread</code> is assigned to or destroyed while being joinable
  there is no throw-expression involved.
</li>
<li>
  In case <code>std::unexpected</code> is called by the program, <code>std::terminate</code> is
  triggered by the implementation - no throw-expression involved.
</li>
<li>
  In case a destructor throws during stack unwinding we have two throw-expressions
  involved.
 </li>
 </ul>
<p>
Which one is referred to?
<p/>
In case <code>std::nested_exception::rethrow_nested</code> is called for an object that has
captured no exception, there is no throw-expression involved directly (and may no throw
be involved even indirectly).
<p/>
Next, 17.9.5.1 <a href="https://wg21.link/terminate.handler">[terminate.handler]</a>, paragraph 2 says
</p>
<blockquote><p>
<i>Required behavior</i>: A <code>terminate_handler</code> shall terminate execution
of the program without returning to the caller.
</p></blockquote>
<p>
This seems to allow that the function may exit by throwing an
exception (because word "return" implies a normal return).
<p/>
One could argue that words "terminate execution of the program" are sufficient,
but then why "without returning to the caller" would be mentioned. In
case such handler throws, noexcept specification in function <code>std::terminate</code>
is violated, and <code>std::terminate</code> would be called recursively - should
<code>std::abort</code> not be called in case of recursive <code>std::terminate</code>
call? On the other hand some controlled recursion could be useful, like in the
<a href="http://cplusplus.co.il/2010/03/21/catching-uncaught-exceptions-within-terminate/">following technique</a>.
</p>
</blockquote>

<p>
The here mentioned wording changes by N3189 in regard to 17.9.5.4 <a href="https://wg21.link/terminate">[terminate]</a> p1
were done for a better separation of effects (Effects element) and additional normative
wording explanations (Remarks element), there was no meaning change intended. Further,
there was already a defect existing in the previous wording, which was not updated when
further situations where defined, when <code>std::terminate</code> where supposed to be
called by the implementation.
<p/>
The part
<p/>
"in effect immediately after evaluating the throw-expression"
<p/>
should be removed and the quoted reference to 17.9.5.1 <a href="https://wg21.link/terminate.handler">[terminate.handler]</a>
need to be part of the effects element where it refers to the current <code>terminate_handler</code>
function, so should be moved just after
<p/>
"Effects: Calls the current <code>terminate_handler</code> function."
<p/>
It seems ok to allow a termination handler to exit via an exception, but the
suggested idiom should better be replaced by a more simpler one based on
evaluating the current exception pointer in the terminate handler, e.g.
</p>
<blockquote><pre>
void our_terminate (void) {
  std::exception_ptr p = std::current_exception();
  if (p) {
    ... // OK to rethrow and to determine it's nature
  } else {
    ... // Do something else
  }
}
</pre></blockquote>

<p><i>[2011-12-09: Daniel comments]</i></p>


<p>
A related issue is <a href="lwg-defects.html#2111" title="Which unexpected&#47;terminate handler is called from the exception handling runtime? (Status: C++17)">2111</a><sup><a href="https://cplusplus.github.io/LWG/issue2111" title="Latest snapshot">(i)</a></sup>.
</p>


<p><i>[2012, Kona]</i></p>

<p>
Move to Open.
</p>
<p>
There is an interaction with Core issues in this area that Jens is already supplying wording
for.  Review this issue again once Jens wording is available.
</p>
<p>
Alisdair to review clause 15.5 (per Jens suggestion) and recommend any changes, then integrate
Jens wording into this issue.
</p>

<p><i>[2024-11-07 Status changed: Open &rarr; Resolved.]</i></p>

<p>Resolved by the resolution of LWG <a href="lwg-defects.html#2111" title="Which unexpected&#47;terminate handler is called from the exception handling runtime? (Status: C++17)">2111</a><sup><a href="https://cplusplus.github.io/LWG/issue2111" title="Latest snapshot">(i)</a></sup>.</p>



<p id="res-2088"><b>Proposed resolution:</b></p>





</body>
</html>
