BEGIN vs BEGIN TRY

  • autoexcrement

    SSCertifiable

    Points: 5723

    I just want to be 100% sure… Do the following two statements execute identically? Meaning, can I use the 2nd one, or do I need the extra BEGIN/END as in the 1st one? Thanks!

    IF @blabla > 0
    BEGIN
      BEGIN TRY
         [do lotsa stuff]
      END TRY
      BEGIN CATCH
         [do stuff]
      END CATCH
    END

    IF @blabla > 0
    BEGIN TRY
         [do lotsa stuff]
    END TRY
    BEGIN CATCH
         [do stuff]
    END CATCH


    "If I had been drinking out of that toilet, I might have been killed." -Ace Ventura

  • Lynn Pettis

    SSC Guru

    Points: 442077

    Try them both and see if either or both work.  Personally, I would lean to using the first format because of the IF.

  • autoexcrement

    SSCertifiable

    Points: 5723

    As far as I can tell, it seems to work the same either way. Just didn’t want to accidentally screw this up.


    "If I had been drinking out of that toilet, I might have been killed." -Ace Ventura

  • Lynn Pettis

    SSC Guru

    Points: 442077

    autoexcrement - Friday, November 9, 2018 1:24 PM

    As far as I can tell, it seems to work the same either way. Just didn't want to accidentally screw this up.

    If you are working in a development or sandbox environment you should screw anything up that can’t be fixed.  Don’t test in production.

  • andycadley

    SSCertifiable

    Points: 5102

    Both will work, though I personally prefer the explicit BEGIN/END pair on any IF statement, if only because somebody will come along later and ask the same question about whether that works or not.

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

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