views:

343

answers:

1

Solution:

I've misinterpreted the example from SQL Books Online. Yes, that below the section Errors Unaffected by a TRY…CATCH Construct ... I'm so sorry :(


I was just trying to execute this simple example in SQL Server Management Studio:

USE AdventureWorks;
GO

BEGIN TRY
    -- Table does not exist; object name resolution
    -- error not caught.
    SELECT * FROM NonexistentTable;
END TRY
BEGIN CATCH
    SELECT 
        ERROR_NUMBER() as ErrorNumber,
        ERROR_MESSAGE() as ErrorMessage;
END CATCH

But the only thing I find as output is:

Msg 208, Level 16, State 1, Line 5 Invalid object name 'NonexistentTable'.

So, the error seems to be captured by the SQL Server Management Studio instead of the catch block, which is obviously not what I expected. Am I missing something?

Thanks in advance :)

+2  A: 

Your first example is used in the SQL Books Online, did you copy it from there? If so, read the entire topic and you'll see that this indeed will not work. The error is not raised by Management Studio, it is a name resolution error and that will simply not be caught by a TRY/CATCH block.

Ooops, yes, you're right. This is my fault for just overlooking documentation. Thanks.
Auron