title: How to catch exceptions conditionally?
body: <p>My large application has this structure:</p>
  
  <pre><code>int main()
  {
      try {
          ...
      } catch (std::exception&amp; e) {
          std::cout &lt;&lt; "Fatal error: " &lt;&lt; e.what() &lt;&lt; (some more data) &lt;&lt; std::endl;
          return 1;
      }
  }
  </code></pre>
  
  <p>Deep inside the call stack, various objects check their internal state, throwing <code>std::runtime_exception</code> if they detect something bad. The all-inclusive exception handler catches it, prints some moderately useful info and terminates the program.</p>
  
  <p>However, when I am debugging under MS Visual Studio, I could benefit from not having any exception handler: Visual Studio has its own, very useful, handler, which stops my application at the place where the exception is thrown, so I can examine what went wrong.</p>
  
  <p>How can I do the catching of my exceptions conditionally?</p>
  
  <p>I tried the following:</p>
  
  <pre><code>    try {
          ...
      } catch (std::exception&amp; e) {
          if (IsDebuggerPresent())
              throw;
          else
              std::cout &lt;&lt; "Fatal error: " &lt;&lt; e.what() &lt;&lt; (some more data) &lt;&lt; std::endl;
      }
  </code></pre>
  
  <p>This gave a weird result: Visual Studio caught the exception that was rethrown and showed me the stack trace at the point where the exception was thrown. However, all objects in my application were apparently destructed, and I couldn't see e.g. local or member variables.</p>
  
  <p>I could make the exception handler conditional on a compilation flag:</p>
  
  <pre><code>#ifdef NDEBUG
      try {
  #endif
          ...
  #ifdef NDEBUG
      } catch (std::exception&amp; e) {
          std::cout &lt;&lt; "Fatal error: " &lt;&lt; e.what() &lt;&lt; (some more data) &lt;&lt; std::endl;
      }
  #endif
  </code></pre>
  
  <p>but this is inconvenient because I have to recompile everything if I want to debug it.</p>
  
  <p>So, how can I make my exception handling conditional (depending on a command-line argument, for example)?</p>