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
 General SQL Server Forums
 New to SQL Server Programming
 try catch not capturing table inexistance error.

Author  Topic 

subhaoviya
Posting Yak Master

135 Posts

Posted - 2010-10-21 : 02:17:38
Hi,
i am trying to delete records from table which is not existing. the catch block not checking it. please let me know the other exception handling method to handle this? i dont want to check object_id in if condition.

BEGIN TRY
BEGIN TRANSACTION -- Start the transaction
-- Delete the Employee's phone numbers
DELETE FROM subhaaa
if @@error<>0
print 's'
COMMIT
END TRY
BEGIN CATCH
-- Whoops, there was an error
IF @@TRANCOUNT > 0
ROLLBACK

-- Raise an error with the details of the exception
DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int
SELECT @ErrMsg = ERROR_MESSAGE(),
@ErrSeverity = ERROR_SEVERITY()
RAISERROR(@ErrMsg, @ErrSeverity, 1)
END CATCH

thanks
subha

Sachin.Nand

2937 Posts

Posted - 2010-10-21 : 03:01:01
The code should give a complilation error if the object subhaaa does not exists.

PBUH

Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2010-10-21 : 04:11:05
Try catch won't catch it, as it is a parse-time error, not en execution time error. As per Books Online:

quote:
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.


--
Gail Shaw
SQL Server MVP
Go to Top of Page
   

- Advertisement -