Raiserror behaviour in TRY-CATCH block

  • Hi all,

    I have a behaviour and cannot explain and would like to get your opinions. I am playing with sqlserver 2012 error handling and notice a different behaviour when using raierror or throw inside a catch block, check this skeleton for a store procedure:

    create procedure p

    as

    BEGIN

    BEGIN TRY (1)

    |insert1

    ||

    |BEGIN TRY (2)

    ||

    ||<sql_code>

    ||

    |END TRY

    |BEGIN CATH (2)

    ||...

    ||raiserrror

    |END CATCH

    ||

    |insert 2

    END TRY

    BEGIN CATCH (1)

    |...

    |raierror/throw

    END CATCH

    |

    insert 3

    END

    What I see is that if there is a exception in inner try block (begin try 2) what happens is:

    - if the outer catch block has raierror then insert 5 is executed but it it is a throw the it isn't

    - insert 2 is not executed even being the same situation (only difference is that it is inner block)

    That is ... it seems that commands after the outermost catch are executed if you have a raiserror in that outermost catch but they aren't if there is a throw instead of raiserror (and this happens only for the most outer catch block, if it is an inner block then both raiserror and throw behaves similar and commands are not executed).

    Any explanation for this ?.

    Thanks a lot in advance.

Viewing 0 posts

You must be logged in to reply to this topic. Login to reply