Exception catching mechanism, C++
<p>Consider this code:</p>

<pre><code>int main()
{
    try 
    {
        throw std::range_error("");
    }
    catch (std::bad_alloc) 
    {
        std::cout &lt;&lt; "AAAA" &lt;&lt; std::endl;
        throw;
    }
    catch (std::range_error) 
    {
        std::cout &lt;&lt; "BBB" &lt;&lt; std::endl;
        throw;
    }
    catch (std::exception)
    {
        std::cout &lt;&lt; "CCC" &lt;&lt; std::endl;
    }
    std::cout &lt;&lt; "DDD" &lt;&lt; std::endl;
}
</code></pre>

<p>Here I throw an exception of type <code>std::range_error</code> and trying to catch it.<br>
Logically the first <code>catch</code> block cannot catch it because of type mismatch(<code>std::bad_alloc</code> and <code>std::range_error</code>).<br>
The second catch block must catch it because they are the same types of <code>std::range_error</code>.<br>
And also, when I rethrow the exception in second catch block it must be caught in the third catch block.</p>

<p>So my output must be </p>

<pre><code>BBB
CCC
DDD
</code></pre>

<p>But I only get the <code>BBB</code> output with the termination.</p>

<p>Can anybody please explain me the behavior??</p>