title: c++ std::ios_base::failure exception
body: <p>Standard(<code>N3337</code>) says (<code>27.5.3.1.1 Class ios_base::failure</code>):</p>
  
  <blockquote>
    <p>The class failure defines the base class for the types of all objects
    thrown as exceptions, by functions in the iostreams library, to report
    errors detected during stream buffer operations.</p>
  </blockquote>
  
  <p>I have a simple test program which emulates restricted resource environment while using of std::ostringstream:</p>
  
  <pre><code>#include &lt;sys/time.h&gt;
  #include &lt;sys/resource.h&gt;
  
  #include &lt;errno.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  #include &lt;iostream&gt;
  #include &lt;sstream&gt;
  
  int main(int argc, const char* argv[])
  {
      rlimit limit;
      limit.rlim_cur = limit.rlim_max = 268435456;
  
      if(setrlimit(RLIMIT_AS, &amp;limit)) {
          std::cerr &lt;&lt; "Cannot set resource limit: " &lt;&lt; strerror(errno) &lt;&lt; std::endl;
          exit(EXIT_FAILURE);
      }
  
      std::ostringstream os;
      os.exceptions(std::ostringstream::badbit);
  
      try {
          auto iterations = 1024 * 1024 * 1024;
  
          while(iterations &amp;&amp; --iterations) os &lt;&lt; 'F';
  
      } catch(const std::ios_base::failure&amp; ex) {
          std::cerr &lt;&lt; "Caught: std::ios_base::failure" &lt;&lt; std::endl;
      } catch(const std::bad_alloc&amp; ex) {
          std::cerr &lt;&lt; "Caught: std::bad_alloc" &lt;&lt; std::endl;
      } catch(...) {
          std::cerr &lt;&lt; "Caught: ellipsis" &lt;&lt; std::endl;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>In my environment (Linux, gcc 5.3.0) I got <code>Caught: std::bad_alloc</code> on <code>stderr</code>. <a href="http://coliru.stacked-crooked.com/a/3431d894273ba4d6" rel="nofollow">One of online compilers</a> shows the same output.</p>
  
  <p>The question is: why exception type is <code>std::bad_alloc</code> and not <code>std::ios_base::failure</code> ?</p>