Reusing exception handling code in C++ : <p>I have these two functions, with duplicated exception treatment, which has the sole purpose of displaying an error message:</p>

<pre><code>void func1() noexcept {
  try {
    do_task();
    do_another_task();
  } catch (const std::out_of_range&amp; e) {
    show_msg("Out of range error", e.what());
  } catch (const std::logic_error&amp; e) {
    show_msg("Logic error", e.what());
  } catch (const std::system_error&amp; e) {
    show_msg("System error", e.what());
  } catch (const std::runtime_error&amp; e) {
    show_msg("Runtime error", e.what());
  } catch (const std::exception&amp; e) {
    show_msg("Generic error", e.what());
  }
}

void func2() noexcept {
  try {
    do_something();
    do_something_else();
    do_even_more();
  } catch (const std::out_of_range&amp; e) {
    show_msg("Out of range error", e.what());
  } catch (const std::logic_error&amp; e) {
    show_msg("Logic error", e.what());
  } catch (const std::system_error&amp; e) {
    show_msg("System error", e.what());
  } catch (const std::runtime_error&amp; e) {
    show_msg("Runtime error", e.what());
  } catch (const std::exception&amp; e) {
    show_msg("Generic error", e.what());
  }
}
</code></pre>

<p>I could just handle <code>std::exception</code> and show a single generic message, but I want to be more specific, that's why I'm catching all possible exceptions.</p>

<p>I want to reuse this exception treatment code. I thought about this:</p>

<pre><code>void run_treated(std::function&lt;void()&gt; func) noexcept {
  try {
    func();
  } catch // ... all the catches go here
}

void func1() noexcept {
  run_treated([]()-&gt;void {
    do_task();
    do_another_task();
  });
}

void func2() noexcept {
  do_something();
  do_something_else();
  do_even_more();
}
</code></pre>

<ol>
<li>Is this a good approach?</li>
<li>If so, <code>run_treated</code> will be called <em>a lot</em>. Should I be concerned about performance?</li>
<li>Any other approaches?</li>
</ol>
