c++ - Does a catch block need to be written immediately after a try block? : <p>My compiler gives me an error for the following code:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;stdexcept&gt;

using namespace std;


void test()
{
     throw runtime_error("Error");
}

int main()
{
     try
     {
          test();
     }

     for (int i = 0; i &lt; 10; i++)
     {

     }

     catch (exception&amp; e)
     {
          cout &lt;&lt; e.what();
     }
}
</code></pre>

<p>It says "error: expected 'catch' before '(' token", and it's referring to the '(' in the for loop initialization.</p>

<p>Do I have to write the catch block immediately after the try block? I thought that if an error is thrown in the try block, the program will bubble out until it finds an appropriate catch block. Why doesn't that apply here?</p>
