Bjorn D. Jensen


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





Re: catch me if you can / bug in the "bug"?

Jens K. Suessmeyer


Wow, those were many samples :-)

But boiling the samples down you are refering to the detail that the not existing table is not recognized at Creation time but recognized at execution time. THis is called defering name resolution where SQL Server resolves the name deferred if the base objects do not exist at runtime.

See also: ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/udb9/html/c9989543-fb85-4ed0-ac67-c626659905e0.htm

If you know the german language and are able to read see this here for more information:

http://www.avanade.com/de/_uploaded/pdf/news/20061106dnmspecialsqlstrfehlerbjenss484804.pdf

Jens K. Suessmeyer

---
http://www.sqlserver2005.de
---







Re: catch me if you can / bug in the "bug"?

Bjorn D. Jensen

Hi!

Vielen dank ;-)

Actually I already had read about defferred name resolution.

My points are:

1. BOL should provide example with call to non existing stored proc, which will end in catch-block!!!

2. I would expect that code control goes to catch block for non existing table/function also (yes, yes it is documented that it doesn't)!

3. I hope that non existing proc also will be catched in future!

Greetings







Re: catch me if you can / bug in the "bug"?

Tom Phillips

That is not a bug, that is how it works.

There are "catchable" errors and non-catchable errors. The "table does not exist" is a non-catchable error. Non-catchable errors simply terminate the batch, or stored proc, and you can do NOTHING about it.

This is a HUGE failing of the error processing in MS SQL. There are 10,000 possible errors in TSQL code and you can actually only "catch" 500 of them.





Re: catch me if you can / bug in the "bug"?

Jens K. Suessmeyer

1. Well actually it does, it states that compilation errors are not caught at all. Like "SELEKT *" (which has a typo and a incomplete syntax)
2. Well, its documented :-) Hey, wait a minute, this is a feature :-)
3.Well it wonĄŻt as it would break users code.






Re: catch me if you can / bug in the "bug"?

Bjorn D. Jensen

Hi!

The german pdf explain exactly why this stuff is so important to me ;-)

I'm happy about what you are saying about "3".

About "1": no BOL has not an example with non existing stored proc, where the error will be catched!

Thanks






Re: catch me if you can / bug in the "bug"?

Jens K. Suessmeyer

Well I upped a presentation for you which I held this year in spring (and actually last year) about Exeception Handling. I have another workshop article in a german magazine next month about that... etc... so any questions do not hesitate to contact me.

http://www.sqlserver2005.de/Link.aspx ID=18