Exception Handling (using Try-Catch) in SQL Statement!
Generally, we use try-catch block in frontend code behind to handle exception, even we try to catch error which occurred at SQL statement. We can easily catch/handle the error at SQL statement, it’s really very easy and good approach to handle EXCEPTION
at SQL statement (it may be in stored procedure or in trigger or in function or in view etc.)
Try-catch statement in SQL statement as bellows:
BEGIN TRY SELECT (1/0) AS DividedByZero END TRY BEGIN CATCH SELECT ERROR_NUMBER() AS ErrorCode,
ERROR_LINE() AS LineNumber,
ERROR_MESSAGE() AS ErrorMessage END CATCH
Md. Elias Hossain
MCP, MCTS(WEB)
Software Engineer
elias_kuet
Member
14 Points
13 Posts
Exception Handling (using Try-Catch) in SQL Statement!
Jan 07, 2010 08:29 AM|LINK
Exception Handling (using Try-Catch) in SQL Statement!
Generally, we use try-catch block in frontend code behind to handle exception, even we try to catch error which occurred at SQL statement. We can easily catch/handle the error at SQL statement, it’s really very easy and good approach to handle EXCEPTION at SQL statement (it may be in stored procedure or in trigger or in function or in view etc.)
Try-catch statement in SQL statement as bellows:
BEGIN TRY
SELECT (1/0) AS DividedByZero
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrorCode,
ERROR_LINE() AS LineNumber,
ERROR_MESSAGE() AS ErrorMessage
END CATCH
MCP, MCTS(WEB)
Software Engineer
jportelas
Contributor
2718 Points
591 Posts
Re: Exception Handling (using Try-Catch) in SQL Statement!
Jan 08, 2010 12:35 PM|LINK
But this applies to SQL Server 2005 and upper right?
elias_kuet
Member
14 Points
13 Posts
Re: Exception Handling (using Try-Catch) in SQL Statement!
Jan 08, 2010 12:57 PM|LINK
Yes, this feature is adopted in SQL SERVER 2005 and later versions.
MCP, MCTS(WEB)
Software Engineer