title: Exception handling placement - C#
body: <p>I've decided to remove some of the using statements in my code so I can <strong>catch specific exceptions</strong> and handle the disposing of resources manually. I have refactored some of the code to make it more readable and maintable, after implementing the new try/catch block I am left wondering if they have been placed correctly for the task at hand.</p>
  
  <p><strong>Example:</strong></p>
  
  <pre><code>public static DataTable Select(string table, string[] column, Object operand)
  {
          DataTable dTable = null;
          SQLiteConnection connection = null;
          SQLiteCommand command = null;
          SQLiteDataReader dReader = null;
  
          //convert to array for arguments
          StringBuilder query = new StringBuilder();
          query.Append("select ");
  
          for (int i = 0; i &lt; column.Length; i++)
          {
              query.Append(column[i]);
  
              if (i &lt; column.Length - 1)
              {
                  query.Append(",");
              }
          }
          query.Append(" from ");
          query.Append(table);
  
          try
          {
              connection = new SQLiteConnection(_connectionString);
              command = new SQLiteCommand(query.ToString(), connection);
              dTable = new DataTable();
  
              connection.Open();
  
              dReader = command.ExecuteReader();
  
              dTable.Load(dReader);
  
              return dTable;
          }
          catch (SQLiteException sqle)
          {
              //Handle exception
          }
          finally
          {
              connection.Dispose();
              command.Dispose();
              dReader.Dispose();
              dTable.Dispose();
          }
          return null;
  }
  </code></pre>
  
  <p>In this example I have only implemented the try/catch around the SQL operations themselves, I did this as it ensures any exceptions that are thrown can be noted and the resourced disposed correctly. I then noticed that this left the for loop open to exceptions, although the supplied indexer will be protected and created via a GUI.</p>
  
  <p>Would I be wise to encapsulate the entire method in the try/catch statement or am I being overly cautious? You could say I'm looking for the best practice when it comes to managing the placement of the statements themselves.</p>
  
  <p>Thanks for your time!</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I know that the using statement would be ideal in terms of handling the disposal and management of resources however as mentioned at the beginning of the question I wish to be able to catch specific types of exceptions, in particular those generated from the SQLite components.</p>