Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Kristen
Test
22859 Posts |
Posted - 2007-02-21 : 21:36:58
|
In SQL 2005 what does TRY ... CATCH actually catch?Basically just the same stuff thatIF @@ERROR <> 0used to catch? or will it also catch, say, attempting to select from a non-existent table, or being the deadlock victim?Background:Many of our SProcs insert a row in a logging table, and then at the end of the SProc update it with the elapsed time.A row which is inserted, and never updated, indicates an Sproc that "failed" catastrophically. If CATCH could catch all of those errors then we could throw the Logging stuff away ... which should save a few CPU cyclesThanksKristen |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-02-21 : 23:06:21
|
| "will it also catch, say, attempting to select from a non-existent table"Selecting from non-existent table will result in Syntax error/compile-time error, so I don't think TRY...CATCH can help you there, of course unless you write your code as D-Sql."or being the deadlock victim?"This can certainly be trapped with TRY...CATCH.See this: [url]http://www.sql-server-performance.com/art_deadlock_sql_2005.asp[/url]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
hrishi_des
Yak Posting Veteran
76 Posts |
Posted - 2007-02-22 : 01:37:41
|
| We have used BEGIN TRY .. CATCH extensively and it helps to catch all errors (except syntax errors :-))When solution is simple, God is answering…. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-02-22 : 01:47:55
|
according to the BOL http://msdn2.microsoft.com/en-us/library/ms175976.aspxquote: A TRY…CATCH construct catches all execution errors that have a severity higher than 10 that do not close the database connection.TRY…CATCH constructs catch unhandled errors from stored procedures or triggers executed by the code in the TRY block. Alternatively, the stored procedures or triggers can contain their own TRY…CATCH constructs to handle errors generated by their code. For example, when a TRY block executes a stored procedure and an error occurs in the stored procedure, the error can be handled in the following ways: * If the stored procedure does not contain its own TRY…CATCH construct, the error returns control to the CATCH block associated with the TRY block that contains the EXECUTE statement. * If the stored procedure contains a TRY…CATCH construct, the error transfers control to the CATCH block in the stored procedure. When the CATCH block code finishes, control is passed back to the statement immediately after the EXECUTE statement that called the stored procedure.Errors Unaffected by a TRY…CATCH ConstructTRY…CATCH constructs do not trap the following conditions: * Warnings or informational messages that have a severity of 10 or lower. * Errors that have a severity of 20 or higher that stop the SQL Server Database Engine task processing for the session. If an error occurs that has severity of 20 or higher and the database connection is not disrupted, TRY…CATCH will handle the error. * Attentions, such as client-interrupt requests or broken client connections. * When the session is ended by a system administrator by using the KILL statement.The following types of errors are not handled by a CATCH block when they occur at the same level of execution as the TRY…CATCH construct: * Compile errors, such as syntax errors, that prevent a batch from running. * Errors that occur during statement-level recompilation, such as object name resolution errors that occur after compilation because of deferred name resolution.These errors are returned to the level that ran the batch, stored procedure, or trigger.
KH |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-02-22 : 05:13:40
|
| OK, that looks helpful, thanks all.Right now we either call an SProc from our application (of course), or the Sproc is called from another Sproc (that was called from the application ...)It is critical that an SProc calling another SProc does a proper error check. I reckon there is a HIGH chance that we have missed some of these. We tend to do:EXEC @intErrNo = dbo.SomeOtherSprocIF @intErrNo <> 0 ... some error routine ...but I'm sure there are plenty of EXEC's that have no test on the result, and anyway what we should be doing isEXEC @intErrNo1 = dbo.SomeOtherSprocSELECT @intErrNo2 = @@ERRORIF @intErrNo <> 0 OR @intErrNo2 <> 0 ... some error routine ...and I know that there are LOTs of places where @@ERROR doesn't get checked.However, for us, the "logging" will bring this to light.So ... it sounds as though a "general purpose" TRY ... CATCH in all SProcs would have a better chance of intercepting things where we get a bit sloppy about checking @@ERROR and stuffIs that a fair assessment?(Plus it will catch DEADLOCK VICTIM, and probably a bunch of others, much more easily than at present)Kristen |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-02-22 : 05:15:49
|
Some trial code, FWIW:CREATE PROCEDURE dbo.k_SP_Test1ASPRINT 'k_SP_Test1 START'SELECT TOP 1 * FROM dbo.NonExistingTablePRINT 'k_SP_Test1 END'RETURN 0GOCREATE PROCEDURE dbo.k_SP_Test2ASPRINT 'k_SP_Test2 START'DECLARE @intErrNo1 int, @intErrNo2 intEXEC @intErrNo1 = dbo.k_SP_Test1SELECT @intErrNo2 = @@ERRORPRINT 'k_SP_Test2 END - @intErrNo1=' + COALESCE(CONVERT(varchar(20), @intErrNo1), '[NULL]') + ', @intErrNo2=' + COALESCE(CONVERT(varchar(20), @intErrNo2), '[NULL]')RETURN 0GOPRINT 'EXEC START'DECLARE @intErrNo1 int, @intErrNo2 intEXEC @intErrNo1 = dbo.k_SP_Test2SELECT @intErrNo2 = @@ERRORPRINT 'EXEC END - @intErrNo1=' + COALESCE(CONVERT(varchar(20), @intErrNo1), '[NULL]') + ', @intErrNo2=' + COALESCE(CONVERT(varchar(20), @intErrNo2), '[NULL]')GODROP PROCEDURE dbo.k_SP_Test1GODROP PROCEDURE dbo.k_SP_Test2GO Kristen |
 |
|
|
|
|
|
|
|