ADO.NET 1.1 Performance Guidelines - Exception Management

From Guidance Share

Jump to: navigation, search

- J.D. Meier, Srinath Vasireddy, Ashish Babbar, and Alex Mackman


Use the ConnectionState property.

Avoid relying on an error handler to detect connection state availability. When you can, use the ConnectionState.Open or ConnectionState.Close method to check the state before use.



Use try/finally to clean up resources

Use try/finally more often than try/catch/finally. Using finally gives you the option to close the connection, even if an exception occurs. If you develop in C#, the using statement provides this functionality with code that is easy to maintain, as shown in the following code fragment.

using( SqlConnection conn = new SqlConnection(
     "server=(local);Integrated Security=SSPI;database=Northwind") )
{ . . . }



Use specific handlers to catch specific exceptions

If you know that there are scenarios where specific errors could possibly occur, use specific handlers. For example, if you want to know if a concurrency violation occurs when multiple updates are occurring, look for exceptions of type DBConcurrencyException. The specific exception classes SqlException and OleDbException provide a detailed message when errors occur. Use this message to log the details. Ensure that specific exceptions precede generic handlers, as shown in the following code fragment.

try
{ ...
}
catch (SqlException sqlex) // specific handler
{ ...
}
catch (Exception ex) // Generic handler
{ ...
}
Personal tools