Testing TRY ... CATCH in SQL Server Management Studio

Decision:

I misinterpreted an example from SQL Books Online. Yes, this is below the section Errors not affected by TRY ... CATCH Construct ... I'm sorry :(


I was just trying to follow 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 appears to be being picked up by SQL Server Management Studio instead of the catch block, which is clearly not what I was expecting. Did I miss something?

Thank you in advance:)

0


source to share


1 answer


Your first example is used in SQL Books Online, did you copy it? If so, read the entire thread and you will see that it really doesn't work. The error is not caused by Management Studio, it is a name resolution error and it just won't be captured by the TRY / CATCH block.



+2


source







All Articles