Why can't I catch this exception?
<p>I want to create a number of exception types which derive from std::runtime_error and I want them to have stringstream type functionality.  I've therefore created an exception class which composes a <code>std::stringstream</code> and which derives from <code>std::runtime_error</code>:</p>

<pre><code>template&lt;typename T&gt;
class error_stream : public std::runtime_error
{
public:
      error_stream() :
      std::runtime_error(""), ss(std::make_shared&lt;std::basic_stringstream&lt;T&gt;&gt; ())
      {
      }

      ~error_stream() throw()
      {
      }

      template &lt;typename T&gt;
      error_stream &amp; operator &lt;&lt; (const T &amp; t)
      {
          *ss &lt;&lt; t;
          return *this;
      }

      virtual const char * what() const throw()
      {
          get_str(s_, ss-&gt;str());
          return s_.c_str();
      }

protected:

    template &lt;typename T&gt;
    void get_str(std::basic_string&lt;char&gt; &amp; s_, const std::basic_string&lt;T&gt; &amp; str) const
    {
        s_ = str;
    }

    template&lt;&gt;
    void get_str(std::basic_string&lt;char&gt; &amp; s_, const std::basic_string&lt;wchar_t&gt; &amp; str) const
    {
        std::basic_string&lt;char&gt; ret(str.begin(), str.end());
        s_ = ret;
    }

protected:
    std::shared_ptr&lt;std::basic_stringstream&lt;T&gt;&gt; ss;
    mutable std::basic_string&lt;char&gt; s_;
};
</code></pre>

<p>And I've created a more specific exception type which in turn derives from this <code>error_stream</code> exception:</p>

<pre><code>template&lt;typename T&gt;
class w32file_exception : public w32utils::error_stream&lt;T&gt; 
{
public:
    w32file_exception() : error_stream&lt;T&gt;() {}
};
</code></pre>

<p>However, I've encountered something I don't understand here, because when I throw a <code>w32file_exception</code> I actually can only catch it as it's parent <code>error_stream</code>.  Can anyone see what I'm doing wrong?</p>

<pre><code>    try
    {
        throw w32file_exception&lt;char&gt;() &lt;&lt; "test";
    }
    catch ( w32file_exception&lt;char&gt; &amp; e )
    {
        ASSERT_PASSED;
    }
    catch ( error_stream&lt;char&gt; &amp; e )
    {
        std::cout &lt;&lt; e.what() &lt;&lt; std::endl;  // Why do I end up here?
    }
</code></pre>
