Hello!
Try this (-:
BEGIN TRY SELECT * FROM NonExistentTable ; END TRY BEGIN CATCH PRINT 'catch me if you can' ; END CATCH ;/*
results in:
Msg 208, Level 16, State 1, Line 2
Invalid object name 'NonExistentTable'.
statement not printet */
BEGIN TRY BEGIN TRY SELECT * FROM NonExistentTable ; END TRY BEGIN CATCH PRINT 'catch me if you can' ; END CATCH ; END TRY BEGIN CATCH PRINT 'catched ' END CATCH ;-- same behaviour
CREATE PROC catchme AS BEGIN BEGIN TRY SELECT * FROM nonexistingtable ; END TRY BEGIN CATCH PRINT 'catch me if you can' END CATCH ; END ; BEGIN TRY EXEC catchme ; END TRY BEGIN CATCH PRINT 'catched' ; END CATCH ;-- prints "catched"
BEGIN TRY EXEC catchnonexistingproc ; END TRY BEGIN CATCH PRINT 'catched again' ; SELECT error_state() END CATCH ;-- prints "catched again"
BEGIN TRY RAISERROR ('catch this' , 10 , 1) ; END TRY BEGIN CATCH PRINT 'hello' ; END CATCH ;
-- no hello
BEGIN TRY RAISERROR ('catch this' , 11 , 1) ; END TRY BEGIN CATCH PRINT 'hello' ; END CATCH ;-- prints hello
CREATE PROC catchme2 AS BEGIN BEGIN TRY RAISERROR ('catch this' , 10 , 1) ; END TRY BEGIN CATCH PRINT 'CATCH ME IF YOU CAN' ; END CATCH ; END ;
BEGIN TRY EXEC catchme2 ; END TRY BEGIN CATCH PRINT 'gotcha ' ; END CATCH ;
-- prints only "catch this"
CREATE PROC catchme3 AS BEGIN BEGIN TRY RAISERROR ('catch this' , 20 , 1) ; END TRY BEGIN CATCH PRINT 'CATCH ME IF YOU CAN' ; END CATCH ; END ; BEGIN TRY EXEC catchme3 ; END TRY BEGIN CATCH PRINT 'gotcha ' ; END CATCH ;
-- prints "CATCH ME IF YOU CAN" ;-)
See also
-- ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/udb9/html/3a5711f5-4f6d-49e8-b1eb-53645181bc40.htm
The above behaviour is as described in BOL, but
comparing to other languages such as Java, C#, PL/SQL
I would expect that catch always catch unless you take the power from the machine, special
in the case for "catchnonexistingproc". Remark the different behaviour compared to "nonexistingtable"
Documentation shows not example for non existing proc!
That catch doesn't catch "is as bug". So is the catch of non existing proc a bug in the bug ;^)
I hope not, because it is the behaviour we want...
Greetings