Using does not throw an exception; it just guarantees a call to .Dispose() .
It's because
using (ResourceType resource = new ResourceType()) equivalent to:
ResourceType resource; try { resource = new ResourceType(); } finally { resource.Dispose(); }
A call to .Dispose() will always be evaluated. The Dispose call is even evaluated if you return to your Using block (before it "really" returns). A Dispose call is even evaluated if an exception is thrown.
However, if an exception is thrown, that exception will still prevent the evaluation of subsequent lines of code (except for .Dispose() , which is always evaluated).
Thus, if an exception occurs, your return will not be returned in any of your statements, but your DataTable will still be deleted.
If you want to guarantee a return, even if an error occurs, you want to do something like this:
List resultsList = new List(); try { using (DataTable resultTable = DBUtility.GetSingleDBTableResult(connectionString, "SELECT * FROM MyDBTable")) { foreach (DataRow dataRow in resultTable.Rows) { resultsList.Add(dataRow[0].ToString()); } } } catch { } return resultsList;
Matt mitchell
source share