Best Practices for Exception Handling and Defensive Programming in Microsoft SQL Server 2005 and 2008
Play Best Practices for Exception Handling and Defensive Programming in Microsoft 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. Exceptions can feel like something that only application code needs to worry about, until you realize that in SQL Server they 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 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. Next, learn how to configure and throw your own custom exceptions, as well as how to leverage the SQL Server exceptions infrastructure with a variety of exception handling and defensive programming techniques - both with and without the TRY/CATCH syntax. Most importantly, we 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.