THROW formatting

  • I've come to the conclusion that I should never answer the Friday QOTD until Monday morning.

  • It's just ridiculous that MS couldn't spend an extra developer day adding formatting to THROW. All they had to do is copy that logic from RAISERROR!

    Gerald Britton, Pluralsight courses

  • Just wanted to add something I actually like about THROW (although it's not compelling enough to use it everywhere). Basically THROW will give you the actual line at which your error occured, whereas RAISERROR will give you the line on which RAISERROR is written.

    You can see by running the following:

    declare @errMess nvarchar(1000),

    @errSev smallint,

    @errState smallint

    begin try

    select 1 / 0

    end try

    begin catch

    select @errMess = ERROR_MESSAGE(),

    @errSev = ERROR_SEVERITY(),

    @errState = ERROR_STATE()

    raiserror(@errMess, @errSev, @errState)

    end catch

    The above reports:

    Msg 50000, Level 16, State 1, Line 12

    Divide by zero error encountered.

    However, run the following:

    begin try

    select 1 / 0

    end try

    begin catch

    throw

    end catch

    And you will see:

    Msg 8134, Level 16, State 1, Line 2

    Divide by zero error encountered.

  • kevin 21984 (10/10/2014)


    Just wanted to add something I actually like about THROW (although it's not compelling enough to use it everywhere). Basically THROW will give you the actual line at which your error occured, whereas RAISERROR will give you the line on which RAISERROR is written.

    You can see by running the following:

    declare @errMess nvarchar(1000),

    @errSev smallint,

    @errState smallint

    begin try

    select 1 / 0

    end try

    begin catch

    select @errMess = ERROR_MESSAGE(),

    @errSev = ERROR_SEVERITY(),

    @errState = ERROR_STATE()

    raiserror(@errMess, @errSev, @errState)

    end catch

    The above reports:

    Msg 50000, Level 16, State 1, Line 12

    Divide by zero error encountered.

    However, run the following:

    begin try

    select 1 / 0

    end try

    begin catch

    throw

    end catch

    And you will see:

    Msg 8134, Level 16, State 1, Line 2

    Divide by zero error encountered.

    Kevin, welcome to the forums!:w00t:

    That is an excellent point, although you can use ERROR_LINE to get the actual line. It's also simpler to just use THROW with no parameters in the CATCH block, instead of having to retrieve ERROR_MESSAGE, ERROR_SEVERITY, ERROR_STATE, and ERROR_LINE and manually put them into the RAISERROR command (using the printf-style formatting to add the ERROR_LINE value to the message).

    It's also always bugged me that RAISERROR is missing an E (it "should have been" RAISEERROR or RAISE_ERROR).:-)

    Steve, thanks for the question.

  • Thanks for the question Steve. I've never used THROW before. Now that I know how it differentiates from RAISERROR, I have some ideas of where it could come in handy.

    Totally agree with others that it seems odd that MS did not take the time to add formatting logic to THROW. I figured that it allowed it and guessed wrong. I also get tripped up every time I type RAISERROR because I want to put the extra "E" in it. Oh well.

  • batgirl (10/10/2014)


    I've come to the conclusion that I should never answer the Friday QOTD until Monday morning.

    +1000

    🙂

    Thanks & Best Regards,
    Hany Helmy
    SQL Server Database Consultant

  • g.britton (10/10/2014)


    It's just ridiculous that MS couldn't spend an extra developer day adding formatting to THROW. All they had to do is copy that logic from RAISERROR!

    Welcome to Microsoft 😀

    Thanks & Best Regards,
    Hany Helmy
    SQL Server Database Consultant

  • comments posted to this topic

  • Looks like THROW just needs a little prep work ahead of time.

Viewing 9 posts - 16 through 23 (of 23 total)

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