Best Practices for Exception Handling and Defensive Programming in SQL Server 2005 and 2008
As developers, we sometimes become lax about dealing with error and exception conditions by the time our code gets down to the data level. These areas feel like something that only application code needs to worry about, until you realize that in SQL Server exceptions can have a tremendous effect on your transactions and your data integrity. Learning to properly handle them is, therefore, of paramount importance to those of us who write data-centric applications. SQL Server 2005 greatly improved exception handling options by adding support for the structured TRY/CATCH syntax, but there is a lot more to the story than just that feature. In this session we will delve into the ins and outs of exceptions in both SQL Server 2005 and SQL Server 2008, starting with the database engine itself: types of exceptions, when and why they're thrown, and how the server treats them. Most importantly, we will review the effect of exceptions on transactions, and how to take programmatic control over the outcome of your transactions in the face of an exception. You will also learn how to throw and configure your own exceptions, as well as a variety of exception handling and defensive programming techniques, both with and without SQL Server's TRY/CATCH syntax.