Sunday, March 11, 2012

Errors from DBCC CHECKDB not caught by CATCH

I'm having problems catching errors from DBCC CHECKDB even though the errors
have a severity level above 10. I'm using the "broken" database created by
Paul Randal,
http://blogs.msdn.com/sqlserverstorageengine/archive/2007/04/17/example-corrupt-database-to-play-with.aspx (see the zip file at the end).
Even though a "DBCC CHECKDB (broken)" results in two errors with severity
level 16 the following code will never end up in the CATCH block..
BEGIN TRY
DBCC CHECKDB(broken)
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() AS ErrorState,
ERROR_PROCEDURE() AS ErrorProcedure,
ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage;
END CATCH
Does anyone know if this is by design or a bug? I'm using SQL Server 2005
with Service Pack 2.Hi Allan,
I've reported it on connect as a bug.
Use @.@.ERROR instead and don't use BEGIN TRY.
The CATCH is never reached, in fact - if you using BEGIN TRY and DBCC
CHECKDB fails then it stays in the BEGIN block and if you check @.@.ERROR its
actually 0!
Tony.
--
Tony Rogerson, SQL Server MVP
http://sqlblogcasts.com/blogs/tonyrogerson
[Ramblings from the field from a SQL consultant]
http://sqlserverfaq.com
[UK SQL User Community]
"Allan" <allan@.newsgroups.nospam> wrote in message
news:B2E2D4EC-7F2D-434C-9F0C-AEA328C57188@.microsoft.com...
> I'm having problems catching errors from DBCC CHECKDB even though the
> errors
> have a severity level above 10. I'm using the "broken" database created by
> Paul Randal,
> http://blogs.msdn.com/sqlserverstorageengine/archive/2007/04/17/example-corrupt-database-to-play-with.aspx
> (see the zip file at the end).
> Even though a "DBCC CHECKDB (broken)" results in two errors with severity
> level 16 the following code will never end up in the CATCH block..
> BEGIN TRY
> DBCC CHECKDB(broken)
> END TRY
> BEGIN CATCH
> SELECT
> ERROR_NUMBER() AS ErrorNumber,
> ERROR_SEVERITY() AS ErrorSeverity,
> ERROR_STATE() AS ErrorState,
> ERROR_PROCEDURE() AS ErrorProcedure,
> ERROR_LINE() AS ErrorLine,
> ERROR_MESSAGE() AS ErrorMessage;
> END CATCH
> Does anyone know if this is by design or a bug? I'm using SQL Server 2005
> with Service Pack 2.

No comments:

Post a Comment