EMartinez
3/21/2007 3:17:00 AM
On Mar 20, 3:56 pm, "John" <nos...@com.com> wrote:
> I have noticed that the addition of a try/catch block alters a specific
> error caused by a stored procedure I have. The following code is a
> simplified version. I understand the error (return with no commit/rollback)
> and have taken steps to fix, but I was wondering if anyone thinks if this is
> a bug or not. Adding a try/catch block to the procedure spATest will result
> in a different error message even though the error in question does not flow
> through the catch handler.
>
> create proc [dbo].[spATest]
> as
> begin
> begin transaction
> return -- this generates an error
> commit transaction
> end
> go
>
> create proc [dbo].[spATest2]
> as
> begin
> BEGIN TRY
> begin transaction
> return -- this generates an error
> END TRY
> BEGIN CATCH
> print 'test' -- will not be executed
> rollback transaction
> END CATCH
> commit transaction
> end
> go
>
> exec spATest
> go
> exec spATest2
> go
>
> yields
>
> Msg 266, Level 16, State 2, Procedure spATest, Line 0
> Transaction count after EXECUTE indicates that a COMMIT or ROLLBACK
> TRANSACTION statement is missing. Previous count = 0, current count = 1.
>
> Msg 0, Level 11, State 0, Line 0
> A severe error occurred on the current command. The results, if any, should
> be discarded.
It seems like the return keyword is in itself, a non-rollback
transaction: which leads me to believe that the try...catch block is
realistically failing to capture the error. Just my thoughts.
Regards,
Enrique Martinez
Sr. SQL Server Developer