title: How to declare a variable in Try and access it in Catch?
body: <p>I am creating an object of my class in try/catch block and want to access it in catch block in case of exception. Even if the object is created ok, I cannot access it in catch block as it declared outside the block.</p>
  
  <pre><code> try {
      MyObject ob(arg1,arg2); //this can trow exception
  
      ob.func1(); //this also can throw exception
  
  } catch (std::exception&amp; ex){
      //I want to access ob here if it was constructed properly and get the reason why func1() failed
  }
  </code></pre>
  
  <p>I can use nested try/catch blocks to solve this issue but is there any other way to solve this</p>
  
  <pre><code>try {
      MyObject ob(arg1,arg2); //this can trow exception
      try {
  
          ob.func1(); //this also can throw exception
      } catch(std::exception&amp; ex) {
          //object was constructed ok, so I can access reason/state why the operation failed
      }
  } catch (std::exception&amp; ex){
      //object failed to construct
  }
  </code></pre>