<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 639: Still problems with exceptions during streambuf IO</title>
<meta property="og:title" content="Issue 639: Still problems with exceptions during streambuf IO">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue639.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#NAD">NAD</a> status.</em></p>
<h3 id="639"><a href="lwg-closed.html#639">639</a>. Still problems with exceptions during streambuf IO</h3>
<p><b>Section:</b> 31.7.5.3.3 <a href="https://wg21.link/istream.extractors">[istream.extractors]</a>, 31.7.6.3.3 <a href="https://wg21.link/ostream.inserters">[ostream.inserters]</a> <b>Status:</b> <a href="lwg-active.html#NAD">NAD</a>
 <b>Submitter:</b> Daniel Kr&uuml;gler <b>Opened:</b> 2007-02-17 <b>Last modified:</b> 2017-04-22</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#istream.extractors">issues</a> in [istream.extractors].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p>
There already exist two active DR's for the wording of  [istream::extractors]/13
from 14882:2003(E), namely <a href="lwg-defects.html#64" title="Exception handling in basic_istream::operator&gt;&gt;(basic_streambuf*) (Status: TC1)">64</a><sup><a href="https://cplusplus.github.io/LWG/issue64" title="Latest snapshot">(i)</a></sup> and <a href="lwg-defects.html#413" title="Proposed resolution to LDR#64 still wrong (Status: CD1)">413</a><sup><a href="https://cplusplus.github.io/LWG/issue413" title="Latest snapshot">(i)</a></sup>.
</p>

<p>
Even with these proposed corrections, already maintained in N2134,
I have the feeling, that the current wording does still not properly
handle the "exceptional" situation. The combination of para 14
</p>

<blockquote><p>
"[..] Characters are extracted and inserted until
any of the following occurs:
</p>
<p>
[..]
</p>
<p>
- an exception occurs (in which case the exception is caught)."
</p></blockquote>

<p>
and 15
</p>

<blockquote><p>
"If the function inserts no characters, it calls setstate(failbit),
which
may throw ios_base::failure (27.4.4.3). If it inserted no characters
because it caught an exception thrown while extracting characters
from *this and failbit is on in exceptions() (27.4.4.3), then the
caught
exception is rethrown."
</p></blockquote>

<p>
both in N2134 seems to imply that any exception, which occurs
*after* at least one character has been inserted is caught and lost
for
ever. It seems that even if failbit is on in exceptions() rethrow is
not
allowed due to the wording "If it inserted no characters because it
caught an exception thrown while extracting".
</p>

<p>
Is this behaviour by design?
</p>

<p>
I would like to add that its output counterpart in 31.7.6.3.3 <a href="https://wg21.link/ostream.inserters">[ostream.inserters]</a>/7-9
(also
N2134) does not demonstrate such an exception-loss-behaviour.
On the other side, I wonder concerning several subtle differences
compared to input::
</p>
<p>
1) Paragraph 8 says at its end:
</p>

<blockquote><p>
"- an exception occurs while getting a character from sb."
</p></blockquote>

<p>
Note that there is nothing mentioned which would imply that such
an exception will be caught compared to  [istream::extractors]/14.
</p>

<p>
2) Paragraph 9 says:
</p>

<blockquote><p>
"If the function inserts no characters, it calls setstate(failbit)
(which
may throw ios_base::failure (27.4.4.3)). If an exception was thrown
while extracting a character, the function sets failbit in error
state,
and if failbit is on in exceptions() the caught exception is
rethrown."
</p></blockquote>

<p>
The sentence starting with "If an exception was thrown" seems to
imply that such an exception *should* be caught before.
</p>


<p id="res-639"><b>Proposed resolution:</b></p>
<p>
(a) In  [istream::extractors]/15 (N2134) change the sentence
</p>

<blockquote><p>
If the function inserts no characters, it calls
<code>setstate(failbit)</code>, which may throw <code>ios_base::failure</code>
(27.4.4.3). If <del>it inserted no characters because it caught an
exception thrown while extracting characters from <code>*this</code></del>
<ins>an exception was thrown while extracting a character from
<code>*this</code>, the function sets <code>failbit</code> in error state,</ins>
and <code>failbit</code> is on in <code>exceptions()</code> (27.4.4.3), then the
caught exception is rethrown.
</p></blockquote>

<p>
(b) In 31.7.6.3.3 <a href="https://wg21.link/ostream.inserters">[ostream.inserters]</a>/8 (N2134) change the sentence:
</p>

<blockquote>
<p>
Gets characters from <code>sb</code> and inserts them in <code>*this</code>.
Characters are read from <code>sb</code> and inserted until any of the
following occurs:
</p>
<ul>
<li>end-of-file occurs on the input sequence;</li>
<li>inserting in the output sequence fails (in which case the character to be inserted is not extracted);</li>
<li>an exception occurs while getting a character from <code>sb</code> <ins>(in which
case the exception is caught)</ins>.</li>
</ul>
</blockquote>



<p><b>Rationale:</b></p><p>
This extractor is described as a formatted input function so the
exception behavior is already specified. There is additional behavior
described in this section that applies to the case in which failbit is
set. This doesn't contradict the usual exception behavior for formatted
input functions because that applies to the case in which badbit is set.
</p>




</body>
</html>
