web training room for online courses
Learn SQL Database Development, MS SQL Tutorial Online

Exception Handling in SQL Server Database, SQL Tutorial

We learn SQL Server Exception Handling with TRY, CATCH , THROW, RAISERROR
Like any other programming language there is a technique of error handling in SQL Server query during Database Development

SQL Server has two types of exceptions:
  • System Defined
  • User Defined

Here are few Error handling property statements in SQL
ERROR_LINE() tell you where the error is, very helpful
ERROR_MESSAGE() this will get the error details

Handling the Exception using TRY CATCH in SQL Database

error handling in sql database

Note: In SQL Server RAISERROR and THROW both statements are used to raise an error. RAISERROR was introduced from SQL Server 7.0, where as the journey of THROW statement has just began with SQL Server 2012

RAISERROR eventually will be replaced by THROW

Now we can use RAISERROR inside catch block like

How to find “Error log file location” on your SQL Server

error log file in sql database
SELECT SERVERPROPERTY('ErrorLogFileName') AS 'Error log file location'

We also can get error log information by executing SP_READERRORLOG ,but to execute this sp user need to have securityadmin role

Name Email Website
SQL Error Handling