Home > DeveloperSection > Forums > SQL Server Exception Handling
Ankit Singh

Total Post:341

Points:2389
Posted on    December-08-2015 9:30 PM

 Database SQL Server  SQL  SQL Server 2008  SQL Server 2012 
Ratings:


 1 Reply(s)
 334  View(s)
Rate this:
I want to use Exception Handling in  SQL Server please help me.


aditya kumar Patel

Total Post:250

Points:1782
Posted on    December-08-2015 9:33 PM

QL Server also has an exception model to handle exceptions and errors that occurs in T-SQL statements. To handle exception in Sql Server we have TRY..CATCH blocks. We put T-SQL statements in TRY block and to handle exception we write code in CATCH block. If there is an error in code within TRY block then the control will automatically jump to the corresponding CATCH blocks.The TRY...CATCH statement which helps us to handle the errors effectively in the back end. This handling of the exception can provide additional information about the errors.

BEGIN TRY

DECLARE @val INT

set @val=100/0

 

END TRY

BEGIN CATCH

SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_SEVERITY() AS ErrorSeverity, ERROR_STATE() AS ErrorState, ERROR_PROCEDURE() AS ErrorProcedure,

ERROR_LINE() AS ErrorLine, ERROR_MESSAGE() AS ErrorMessage

END CATCH

GO


Modified On Dec-08-2015 09:34:17 PM

Don't want to miss updates? Please click the below button!

Follow MindStick