title: What is the purpose of a function try block?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/5612486/when-is-a-function-try-block-useful">When is a function try block useful?</a><br>
    <a href="https://stackoverflow.com/questions/6756931/difference-between-try-catch-syntax-for-function">Difference between try-catch syntax for function</a>  </p>
  </blockquote>
  
  
  
  <p>This code throws an <code>int</code> exception while constructing the <code>Dog</code> object inside class <code>UseResources</code>. The <code>int</code> exception is caught by a normal <code>try-catch</code> block and the code outputs :</p>
  
  <pre><code>Cat()  
  Dog()  
  ~Cat()  
  Inside handler
  </code></pre>
  
  <p></p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class Cat
  {
      public:
      Cat() { cout &lt;&lt; "Cat()" &lt;&lt; endl; }
      ~Cat() { cout &lt;&lt; "~Cat()" &lt;&lt; endl; }
  };
  
  class Dog
  {
      public:
      Dog() { cout &lt;&lt; "Dog()" &lt;&lt; endl; throw 1; }
      ~Dog() { cout &lt;&lt; "~Dog()" &lt;&lt; endl; }
  };
  
  class UseResources
  {
      class Cat cat;
      class Dog dog;
  
      public:
      UseResources() : cat(), dog() { cout &lt;&lt; "UseResources()" &lt;&lt; endl; }
      ~UseResources() { cout &lt;&lt; "~UseResources()" &lt;&lt; endl; }
  };
  
  int main()
  {
      try
      {
          UseResources ur;
      }
      catch( int )
      {
          cout &lt;&lt; "Inside handler" &lt;&lt; endl;
      }
  }
  </code></pre>
  
  <p>Now, if we replace the definition of the <code>UseResources()</code> constructor, with one that uses a <code>function try block</code>, as below,</p>
  
  <pre><code>UseResources() try : cat(), dog() { cout &lt;&lt; "UseResources()" &lt;&lt; endl; } catch(int) {}
  </code></pre>
  
  <p>the output is the same</p>
  
  <pre><code>Cat()  
  Dog()  
  ~Cat()  
  Inside handler 
  </code></pre>
  
  <p>i.e., with exactly the same final result.</p>
  
  <p>What is then, the purpose of a <code>function try block</code> ?</p>