Testing TRY…CATCH in SQL Server Management Studio


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;

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

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 :)


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.

Need Your Help

How to write coercion methods

r dispatch coercion s4 reference-class

I'm having a bunch of custom-made Reference Classes and would like to write coercion methods for some of them. It'd be nice if a function call would look like this:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.