title: std::rethrow_exception and thrown exception type
body: <p>In a library I have a hierarchy of exceptions (<code>RuntimeException -&gt; RecognitionException -&gt; NoViableAltException</code>). During execution, a <code>NoViableAltException</code> is thrown, which is caught as below and an exception pointer is created from that:</p>
  
  <pre><code>try {
     //code that throws NoViableAltException
  } catch (RecognitionException &amp;re) {
    std::exception_ptr e = std::make_exception_ptr(re);
    reportError(e);
  }
  </code></pre>
  
  <p>The value of <code>e</code> is used in other places, irrelevant for this question. In <code>reportError()</code> I actually handle the error:</p>
  
  <pre><code>void reportError(std::exception_ptr e) {
  ...
    try {
      std::rethrow_exception(e);
    } catch (NoViableAltException &amp;ne) {
      reportNoViableAlternative(recognizer, ne);
    } catch (InputMismatchException &amp;ne) {
      reportInputMismatch(recognizer, ne);
    } catch (FailedPredicateException &amp;ne) {
      reportFailedPredicate(recognizer, ne);
    } catch (RecognitionException &amp;ne) {
      recognizer-&gt;notifyErrorListeners(ne.getOffendingToken(), ne.what(), e);
    }
  }
  </code></pre>
  
  <p>and here's my problem: when I rethrow <code>e</code>, the <code>NoViableAltException</code> branch is not taken, but that of the <code>RecognitionException</code> (the last one). That is surprising and I wonder why this happens. I also tried to catch <code>NoViableAltException*</code>, to no avail. What is the correct approach to catch the individual exception types?</p>