title: "Terminate called after throwing an instance of 'std::__ios_failure'" when trying to catch exception from `boost::asio::ip::tcp::iostream`
body: <p>I wrote a simple blocking server which waits for a single client and floods it.</p>
  <p>I use <code>boost::asio::ip::tcp::iostream</code> class for interacting with the client as I want to use formatted I/O in the future. I also would like to use exceptions for error handling instead of manual flags checks after each operation, so I called <code>.exceptions()</code> in a fashion similar to <a href="https://en.cppreference.com/w/cpp/io/basic_ios/exceptions" rel="nofollow noreferrer"><code>basic_ios::exceptions</code></a> (I assume Boost derives from the latter, doesn't it?).</p>
  <p>Here is the resulting code:</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;boost/asio.hpp&gt;
  #include &lt;exception&gt;
  #include &lt;iostream&gt;
  #include &lt;sstream&gt;
  #include &lt;utility&gt;
  
  using boost::asio::ip::tcp;
  
  int main() {
      try {
          try {
              boost::asio::io_context io_context;
              tcp::acceptor acceptor(io_context, tcp::endpoint(tcp::v4(), 10000));
              std::cout &lt;&lt; &quot;Listening at &quot; &lt;&lt; acceptor.local_endpoint() &lt;&lt; &quot;\n&quot;;
  
              tcp::iostream client(acceptor.accept());
              client.exceptions(std::ios_base::failbit | std::ios_base::badbit |
                                std::ios_base::eofbit);  // (1)
              while (client &lt;&lt; &quot;x\n&quot;) {
              }
              std::cout &lt;&lt; &quot;Completed\n&quot;;
          } catch (std::exception &amp;e) {
              std::cout &lt;&lt; &quot;Exception: &quot; &lt;&lt; e.what() &lt;&lt; &quot;\n&quot;;
          }
      } catch (...) {
          std::cout &lt;&lt; &quot;Unknown exception\n&quot;;
      }
      return 0;
  }
  
  </code></pre>
  <p>Unfortunately, it looks like some exceptions are still falling through the cracks. If I start the server, connect via <code>nc localhost 10000</code>, look at <code>x</code>s for a while, and then Ctrl+C the client, terminating it, the server <code>std::terminate</code>s with the following error message:</p>
  <pre><code>This application has requested the Runtime to terminate it in an unusual way.
  Please contact the application's support team for more information.
  terminate called after throwing an instance of 'std::__ios_failure'
    what():  basic_ios::clear: iostream error
  </code></pre>
  <p>If I comment out the <code>(1)</code> line, everything works flawlessly and the server terminates with a <code>Completed</code> message when the client disconnects.</p>
  <p>I've tried it both on Windows (<code>g++ (Rev6, Built by MSYS2 project) 10.2.0</code> and Boost 1.75.0-3) and Ubuntu 20.04 (<code>g++-10 (Ubuntu 10.2.0-5ubuntu1~20.04) 10.2.0</code> and Boost 1.71.0.0ubuntu2).</p>
  <p>What am I doing wrong?</p>
  <p>GDB session on Ubuntu shows that the exception is thrown from inside a destructor, which is presumably <code>noexcept</code>, which is kind of weird:</p>
  <pre><code>#0  0x00007ffff7e86762 in __cxa_throw () from /lib/x86_64-linux-gnu/libstdc++.so.6
  #1  0x00007ffff7e7dc63 in std::__throw_ios_failure(char const*) () from /lib/x86_64-linux-gnu/libstdc++.so.6
  #2  0x00007ffff7ef0dd2 in std::basic_ios&lt;char, std::char_traits&lt;char&gt; &gt;::clear(std::_Ios_Iostate) ()
     from /lib/x86_64-linux-gnu/libstdc++.so.6
  #3  0x00007ffff7f0f28e in std::ostream::sentry::~sentry() () from /lib/x86_64-linux-gnu/libstdc++.so.6
  #4  0x00007ffff7f0f975 in std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::__ostream_insert&lt;char, std::char_trait
  s&lt;char&gt; &gt;(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, char const*, long) ()
     from /lib/x86_64-linux-gnu/libstdc++.so.6
  #5  0x00007ffff7f0fd5c in std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::operator&lt;&lt; &lt;std::char_traits&lt;char&gt; &gt;(s
  td::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, char const*) () from /lib/x86_64-linux-gnu/libstdc++.so.6
  #6  0x000055555555d3a8 in main () at test.cpp:19
  </code></pre>