
So, if an error would have happened inside this TRY statement, the control would have immediately get transferred to the CATCH statement and then it would have started executing code line by line. We have two blocks of code:Īnything between the BEGIN TRY and END TRY is the code that we want to monitor for an error.

On top of that, there is a RAISERROR function that can be used to generate our own custom error messages which is a great way to translate confusing error messages into something a little bit more meaningful that people would understand. This is a very simple yet structured way of doing it and once you get the hang of it, it can be quite helpful in many cases. Furthermore, the method will be explained in a SQL Server case using a group of T-SQL statements/blocks, which is basically SQL Server way of handling errors. In this article, we’ll take a closer look at the TRY… CATCH statement: the syntax, how it looks, how it works and what can be done when an error occurs. Luckily, we have a chance to translate those messages into something more meaningful to pass on to the users, developers, etc. It can even be translating the error in SQL language because we all know how technical SQL Server error messages could get making no sense and hard to understand. SQL Server error handling can be as simple as just logging that something happened, or it could be us trying to fix an error. For example, when things go wrong, we get a chance to do something about it and possibly make it right again. Error handling in SQL Server gives us control over the Transact-SQL code.
