Error Handling

Learn how to write resilient queries by handling potential errors.

We'll cover the following

A T-SQL query, like any other code, can result in both expected and unexpected errors. When some kind of error is expected, we can account for it. For example, before inserting a new row, we might manually check to see whether the same data exists to avoid primary key violations. However, not all errors can be foreseen. To avoid crashing our stored procedures, functions, or other queries, a more universal approach is to add error handling to our code. T-SQL has a TRYCATCH construct for error-prone scenarios.

Get hands-on with 1200+ tech skills courses.