C# – try/catch + using, right syntax

ctry-catchusing-statement

Which one:

using (var myObject = new MyClass())
{
   try
   {
      // something here...
   }
   catch(Exception ex)
   {
      // Handle exception
   }
}

OR

try
{
   using (var myObject = new MyClass())
   {
      // something here...
   }
}
catch(Exception ex)
{
   // Handle exception
}

Best Answer

I prefer the second one. May as well trap errors relating to the creation of the object as well.