October 10, 2014 at 7:09 am
I've come to the conclusion that I should never answer the Friday QOTD until Monday morning.
October 10, 2014 at 7:14 am
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
October 10, 2014 at 7:50 am
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.
October 10, 2014 at 8:45 am
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.
October 10, 2014 at 9:28 am
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.
October 13, 2014 at 3:25 am
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
October 13, 2014 at 3:28 am
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
October 16, 2014 at 11:45 pm
comments posted to this topic
October 21, 2014 at 6:45 am
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