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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 TRY CATCH block not cathching errors

Author  Topic 

umertahir
Posting Yak Master

154 Posts

Posted - 2009-10-02 : 05:23:21
Hi,

I have got a TRY CATCH block on a sql statement which works fine for other major errors but when the table does not exist then the error is not caught.


BEGIN TRY
-- put table name here which does not exist and the error will not be caught in catch block
DELETE
FROM [Put Any TableName Which Does Not Exist In the Database]

END TRY
BEGIN CATCH
PRINT 'Error: ' + ERROR_MESSAGE()
END CATCH


Result:
Msg 208, Level 16, State 1, Line 3
Invalid object name 'Put Any TableName Which Does Not Exist In the Database'.



I want the above error to be caught in CATCH block.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-10-02 : 06:19:30
nope. these are parse errors which will thrown even before execution starts and hence wont be handled by catch block
Go to Top of Page

umertahir
Posting Yak Master

154 Posts

Posted - 2009-10-02 : 06:43:30
When the same code is put in a stored procedure then the procedure does compile it and not complain about the table not found.
But when the procedure is executed then as soon as it comes to this line of code then it crashes but it executes all the prior sql statements.

quote:
Originally posted by visakh16

nope. these are parse errors which will thrown even before execution starts and hence wont be handled by catch block

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-10-02 : 06:46:51
yup. because it wont check the existence of table until it gets executed
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2009-10-02 : 13:12:33
Just to follow up on what Visakh said, here some info from BOL:
quote:
Errors Unaffected by a TRY…CATCH Construct

TRY…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.


Go to Top of Page
   

- Advertisement -