In this article you will learn about exception handling in T-SQL.
Since it is one of my favorite topics under SQL Server trainings, I always love to talk about Error and Exception handling.As a good programmer we always say that our software must be error and bug free. But in my knowledge no software in the world is 100% error free or bug free.Mistakes are bound to happen since we are humans. So, this article is typically for beginners and junior programmers and beginners to T-SQL.Consider a scenario where you are trying to save some data by using a .Net form that resulted in the following error and you see the following error in the browser.Now, if I think from an end-user prospective I do not know:
Because my end-users are not technical people.What did I do? I was just trying to fill in some details using some form and clicked on the Save/Submit button and things went wrong.So, instead of this if we show a meaningful error message to the end user that will the better option and guide him about how to avoid this situation again.So here we need error and exception handling.Here we are now discussing exception handling in SQL.Please consider the following example. Now I am intentionally trying to divide 1 by 0 and that is incorrect. So the output is an error.Now, if I use some exception handling mechanism provided by T-SQL then it is easy for us to handle any exception.So, in T-SQL we have try and catch blocks for exception handling that is different in syntax from C#.Here we use the following syntax.
Tips:
Building Standalone Data-Driven Applications using SQL Server Compact 4.0