THROW vs RAISERROR with bubbling up

  • Mark Eckeard

    Hall of Fame

    Points: 3462

    Hi all,

    I know THROW is newer and recommended but I'm wondering if one bubbles up and the other doesn't.  Specifically, let's say I have nested Try/Catch's and the error happens in the second one.  Which bubbles up?  Or does the Catch handle that?

    Thanks!

  • Mike01

    SSChampion

    Points: 11284

    Begin try
    select 1/1
    begin try
    select 1/0
    end try
    begin catch
    ;Throw 51000, 'inner catch', 127
    end catch
    end try
    begin catch
    ;Throw 51000, 'outer catch', 127
    end catch

    • This reply was modified 1 month, 2 weeks ago by  Mike01.

    For better, quicker answers, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • rick.thorington

    Valued Member

    Points: 74

    Not the original question, but bubbling up unchanged error is another advantage of THROW. If you want to bubble up the error, unchanged, you can simply call THROW, and it will re-throw the original error -- error number, line number, and message.

    With RAISERROR, you can capture and return the original error message, but it will return 50000 as the messageid (unless you use a stored messageid).

    Converting Mike01's script to pass through original error:

    begin try
    select 1/1
    begin try
    select 1/0
    end try
    begin catch
    throw;
    end catch
    end try
    begin catch
    throw;
    end catch
  • Phil Parkin

    SSC Guru

    Points: 244733

    Throw bubbles up, but only if you choose to do so ... you have control

    BEGIN TRY
    BEGIN TRY
    BEGIN TRY
    SELECT 1 / 0;
    END TRY
    BEGIN CATCH
    THROW 52000, 'Non-Bubbler', 1;
    END CATCH;
    END TRY
    BEGIN CATCH
    PRINT 'Not bubbling';
    END CATCH;
    END TRY
    BEGIN CATCH
    THROW;
    END CATCH;

    BEGIN TRY
    BEGIN TRY
    BEGIN TRY
    SELECT 1 / 0;
    END TRY
    BEGIN CATCH
    THROW 52000, 'Here''s a Bubbler', 1;
    END CATCH;
    END TRY
    BEGIN CATCH
    THROW;
    END CATCH;
    END TRY
    BEGIN CATCH
    THROW;
    END CATCH;

    If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.
    See https://www.sqlservercentral.com/articles/forum-etiquette-how-to-post-datacode-on-a-forum-to-get-the-best-help/ for details of how to post T-SQL code-related questions.

  • Mike01

    SSChampion

    Points: 11284

    THROW 52000, 'Here''s a Bubbler', 1;

     

    Phil, In MA we call that a Bubbla

    For better, quicker answers, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

Viewing 5 posts - 1 through 5 (of 5 total)

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