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 2008 Forums
 Transact-SQL (2008)
 Catch Error

Author  Topic 

jooorj
Posting Yak Master

126 Posts

Posted - 2011-04-26 : 03:55:27
I have the following T-sql
DECLARE @Probs int
BEGIN TRY
SELECT 'This will work'
BEGIN TRY
SELECT * FROM #Temp
END TRY
BEGIN CATCH
SELECT 'The second catch block'
END CATCH
SELECT 'And then this will now work'
END TRY
BEGIN CATCH
SELECT 'An error has occurred at line ' +
LTRIM(STR(ERROR_LINE())) +
' with error ' + LTRIM(STR(ERROR_NUMBER())) + ' ' + ERROR_MESSAGE()
END CATCH

Now the catch does not fired why?

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2011-04-26 : 04:10:08
Because there's no run-time error there.

A missing object is a parse-time error. Parse-time errors cannot be caught by a try-catch.

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

jooorj
Posting Yak Master

126 Posts

Posted - 2011-04-26 : 04:21:46
then how to let catch executed, what is the true solution ?
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2011-04-26 : 04:25:22
Catch does not and cannot catch parse-time errors (object does not exist, column does not exist). It catches run-time errors. So if you have pk violations, fk violations, deadlocks, data type conversion, etc, those will be caught in a catch block.

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

jooorj
Posting Yak Master

126 Posts

Posted - 2011-04-26 : 04:31:34
Big thanx,
Now I want to list all parse-time errors? what are they ?
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2011-04-26 : 04:37:59
Object does not exist
Column does not exist
Syntax error.

The 2nd and 3rd will be thrown before execution starts. The 1st will only be thrown when that statement is reached due to deferred name resolution.

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

jooorj
Posting Yak Master

126 Posts

Posted - 2011-04-26 : 04:42:50
Is this a bug on SQL server (2005/2008) ?
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2011-04-26 : 05:00:29
No.

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

- Advertisement -