<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2349: Clarify input/output function rethrow behavior</title>
<meta property="og:title" content="Issue 2349: Clarify input/output function rethrow behavior">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2349.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="2349"><a href="lwg-defects.html#2349">2349</a>. Clarify input/output function rethrow behavior</h3>
<p><b>Section:</b> 31.7.5.3.1 <a href="https://wg21.link/istream.formatted.reqmts">[istream.formatted.reqmts]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Zhihao Yuan <b>Opened:</b> 2013-12-06 <b>Last modified:</b> 2022-11-22</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#istream.formatted.reqmts">issues</a> in [istream.formatted.reqmts].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The formatted input function requirement says in 31.7.5.3.1 <a href="https://wg21.link/istream.formatted.reqmts">[istream.formatted.reqmts]</a>:
</p>
<blockquote><p>
"If an exception is thrown during input then <code>ios::badbit</code> is turned on
in <code>*this</code>'s error state.  If <code>(exceptions()&amp;badbit) != 0</code> then the exception
is rethrown."
</p></blockquote>
<p>
while some formatted function may throw an exception from <code>basic_ios::clear</code>, for example
in 22.9.4 <a href="https://wg21.link/bitset.operators">[bitset.operators]</a> p6:
</p>
<blockquote><p>
"If no characters are stored in <code>str</code>, calls <code>is.setstate(ios_base::failbit)</code> (which may 
throw <code>ios_base::failure</code>)"
</p></blockquote>
<p>
So should this exception be considered as "an exception [...] thrown
during input"?  And here is an implementation divergence (or you
can read the following as "a bug libc++ only has" :)
</p>
<blockquote><pre>
cin.exceptions(ios_base::failbit);
bitset&lt;N&gt; b;
try {
  cin &gt;&gt; b;  // type 'a' and return
} catch (...)
{}
</pre></blockquote>
<p>
Now <code>cin.rdstate()</code> is just <code>failbit</code> in libstdc++ (and Dinkumware, by
PJ), but <code>failbit &amp; badbit</code> libc++. Similar difference found in other
places, like <code>eofbit &amp; badbid</code> after <code>std::getline</code>.
<p/>
PJ and Matt both agree that the intention (of <code>badbit</code> + rethrow) is
"to signify an exception arising in user code, not the iostreams package".
<p/>
In addition, I found the following words in unformatted input
function's requirements (31.7.5.4 <a href="https://wg21.link/istream.unformatted">[istream.unformatted]</a>):
</p>
<blockquote><p>
If an exception is thrown during input then <code>ios::badbit</code> is turned on
in <code>*this</code>'s error state. (Exceptions thrown from <code>basic_ios&lt;&gt;::clear()</code>
are not caught or rethrown.)  If <code>(exceptions()&amp;badbit) != 0</code> then the
exception is rethrown.
</p></blockquote>
<p>
The content within the parenthesis is added by LWG defect <a href="lwg-defects.html#61" title="Ambiguity in iostreams exception policy (Status: TC1)">61</a><sup><a href="https://cplusplus.github.io/LWG/issue61" title="Latest snapshot">(i)</a></sup>,
and does fix the ambiguity. However, it only fixed the 1 of 4
requirements, and it lost some context (the word "rethrown" is not
seen before this sentence within this section).
</p>

<p><i>[Lenexa 2015-05-07: Marshall to research and report]</i></p>

<p><i>[Kona 2022-11-08; this would be resolved by <a href="https://wg21.link/P1264" title=" Revising the wording of stream input operations">P1264</a>]</i></p>

<p><i>[2022-11-22 Resolved by <a href="https://wg21.link/P1264R2" title=" Revising the wording of stream input operations">P1264R2</a> accepted in Kona. Status changed: Open &rarr; Resolved.]</i></p>



<p id="res-2349"><b>Proposed resolution:</b></p>
<p>This wording is relative to N3797.</p>

[<i>Drafting note:</i> The editor is kindly asked to introduce additional spaces at the following marked occurrences of
<code>operator&amp;</code> &mdash; <i>end drafting note</i>]

<ol>
<li><p>Modify 31.7.5.3.1 <a href="https://wg21.link/istream.formatted.reqmts">[istream.formatted.reqmts]</a> p1 as indicated:</p>

<blockquote><p>
-1- Each formatted input function begins execution by constructing an object of class <code>sentry</code> with the <code>noskipws</code>
(second) argument false. If the <code>sentry</code> object returns true, when converted to a value of type <code>bool</code>, the
function endeavors to obtain the requested input. If an exception<ins>, other than the ones thrown from <code>clear()</code>, if any,</ins> 
is thrown during input then <code>ios::badbit</code>
is turned on[Footnote 314] in <code>*this</code>'s error state. If <code>(exceptions()<ins> </ins>&amp;<ins> </ins>badbit) != 0</code> 
then the exception is rethrown.
In any case, the formatted input function destroys the <code>sentry</code> object. If no exception has been thrown, it returns <code>*this</code>.
</p></blockquote>
</li>

<li><p>Modify 31.7.6.3.1 <a href="https://wg21.link/ostream.formatted.reqmts">[ostream.formatted.reqmts]</a> p1 as indicated:</p>

<blockquote><p>
-1- Each formatted output function begins execution by constructing an object of class <code>sentry</code>. If this object
returns true when converted to a value of type <code>bool</code>, the function endeavors to generate the requested
output. If the generation fails, then the formatted output function does <code>setstate(ios_base::failbit)</code>,
which might throw an exception. If an exception<ins>, other than the ones thrown from <code>clear()</code>, if any,</ins> is thrown 
during output, then <code>ios::badbit</code> is turned on[Footnote 327]
in <code>*this</code>'s error state. If <code>(exceptions()<ins> </ins>&amp;<ins> </ins>badbit) != 0</code> then the exception is rethrown. 
Whether or not
an exception is thrown, the <code>sentry</code> object is destroyed before leaving the formatted output function. If no
exception is thrown, the result of the formatted output function is <code>*this</code>.
</p></blockquote>
</li>

<li><p>Modify 31.7.6.4 <a href="https://wg21.link/ostream.unformatted">[ostream.unformatted]</a> p1 as indicated:</p>

<blockquote><p>
-1- Each unformatted output function begins execution by constructing an object of class <code>sentry</code>. If this object
returns true, while converting to a value of type <code>bool</code>, the function endeavors to generate the requested
output. If an exception<ins>, other than the ones thrown from <code>clear()</code>, if any,</ins> is thrown during output, 
then ios::badbit is turned on[Footnote 330] in <code>*this</code>'s error state.
If <code>(exceptions() &amp; badbit) != 0</code> then the exception is rethrown. In any case, the unformatted output
function ends by destroying the <code>sentry</code> object, then, if no exception was thrown, returning the value specified
for the unformatted output function.
</p></blockquote>
</li>

<li><p>Modify 31.7.5.4 <a href="https://wg21.link/istream.unformatted">[istream.unformatted]</a> p1 as indicated:</p>

<blockquote><p>
-1- Each unformatted input function begins execution by constructing an object of class <code>sentry</code> with the default
argument <code>noskipws</code> (second) argument true. If the <code>sentry</code> object returns true, when converted to a value
of type <code>bool</code>, the function endeavors to obtain the requested input. Otherwise, if the <code>sentry</code> constructor exits
by throwing an exception or if the sentry object returns false, when converted to a value of type <code>bool</code>, the
function returns without attempting to obtain any input. In either case the number of extracted characters
is set to <code>0</code>; unformatted input functions taking a character array of non-zero size as an argument shall also
store a null character (using <code>charT()</code>) in the first location of the array. If an exception<ins>, other than the 
ones thrown from <code>clear()</code>, if any,</ins> is thrown during input
then <code>ios::badbit</code> is turned on[Footnote 317] in <code>*this</code>'s error state. <del>(Exceptions thrown from 
<code>basic_ios&lt;&gt;::clear()</code> are not caught or rethrown.)</del> If <code>(exceptions()<ins> </ins>&amp;<ins> </ins>badbit) != 0</code> 
then the exception is rethrown. It also counts the number of characters extracted. If no exception has been thrown it ends 
by storing the count in a member object and returning the value specified. In any event the <code>sentry</code> object is destroyed 
before leaving the unformatted input function.
</p></blockquote>
</li>

</ol>






</body>
</html>
