April 2, 2013 at 8:54 am
Hugo Kornelis (4/2/2013)
Good question, but the explanation falls a bit short. I was very disappointed when I first saw this behaviour. A semicolon is a statement terminator and should be used after every statement - but apparently, END TRY is not considered a statement by this logic. So the semicolon after END TRY is not interpreted as terminating the END TRY, but as terminating a dummy, zero-length statement. (They exist. Try typing a bunch of semicolons and hit execute - it will complete without errors).In short - just after I had learned myself to consistently semicolon-terminate every statement, I now have to leanr myself NOT to do that for at least one statement (END TRY).
Thanks Hugo! I was scratching my head TRYing to figure this one out. Also another interesting semi-colon behavior, you must end the statement prior to the THROW with a semi-colon. I received an interesting error (on SQL 2012) when upping the severity and removing all semi-colons.
Msg 6401, Level 16, State 1, Line 13
Cannot roll back THROW. No transaction or savepoint of that name was found.
April 2, 2013 at 9:44 am
I like to terminate each statement with a semicolon and I made this mistake years ago: "Experience is an euphemism for our past mistakes."
Thanks, Venkat!
April 2, 2013 at 2:15 pm
Revenant (4/2/2013)
I like to terminate each statement with a semicolon and I made this mistake years ago: "Experience is an euphemism for our past mistakes."
+1
I had the same tendency and made this mistake some time back. Had to do a lot of googling and reading to understand about the invisible statement between the End Try and Begin Catch other than the ; thats causing the problem..
___________________________________________________________________
If I can answer a question then anyone can answer it..trying to reverse the logic.. :hehe:
April 2, 2013 at 8:54 pm
Good question. Thanks for posting!
April 3, 2013 at 1:18 am
Good question - Had to think hard about that one...
April 3, 2013 at 1:42 am
Von Microsoft am 29.12.2009 um 12:11 bereitgestellt
I am resolving the corresonding SQL11 bug as "by design". Here is the explanation: The semicolon between END TRY and BEGIN CATCH should not be allowed, because they are actually not different statements, but parts of the same TRY-CATCH statement. We only allow semicolons when they separate two statements in a sequence. A word of explanation why then we allow semicolons after BEGIN TRY and BEGIN CATCH. These keywords serve as opening "parentheses" that start an embedded statement sequence. Semicolons after BEGIN TRY/BEGIN CATCH get parsed as part of that embedded sequence, with the first statement in the sequence being empty. While we allow this syntax, I would not recommend it as a good coding practice because it creates a wrong impression of BEGIN TRY/BEGIN CATCH being independent, standalone statements.
Best Regards,
Chris Büttner
April 3, 2013 at 11:46 am
Chris,
Great reference, that clears up the whole conversation 😀
April 4, 2013 at 8:30 am
Revenant (4/2/2013)
I like to terminate each statement with a semicolon and I made this mistake years ago: "Experience is an euphemism for our past mistakes."Thanks, Venkat!
Surely our past mistakes are some of our experiences, so it's not a euphemism at all.
April 4, 2013 at 9:00 am
I had forgotten were not there that the ";" is not alowed in there.
And didn't even notice that THROW would cause an error on three of the four releases the question says it applies to.
Good question.
MS clealrly don't understand error management, if they did they would never have (a) introduced try...catch without including throw in the package or (b) excluded if...else statements and so on from the scope of try-catch. At least they got round to fixing (a) only seven years after they committed that error; but everyone still has to live with (b). Unless of course the statement in BoL is wrong!
Tom
April 5, 2013 at 11:56 am
Nice!
Not all gray hairs are Dinosaurs!
April 10, 2013 at 11:22 am
I do not know much about Try, Catch and Throw. But I got rid of all of the semicolons and the code ran without an error on MS-SMS 2008.
April 23, 2013 at 12:28 am
Bob Cullen-434885 (4/2/2013)
I selected the syntax error option but not for the reason stated. The question states SQL 2005 and higher, but THROW evidently makes its appearance with SQL 2012, so is invalid syntax is the right answer for 2005, 2008, 2008 R2, eh?So, another case of "it depends".
+1.
I chose the correct answer for same reason. THROW is available only in SQL 2012. Apart from that better to know new things.
April 23, 2013 at 12:36 am
Christian Buettner-167247 (4/3/2013)
Von Microsoft am 29.12.2009 um 12:11 bereitgestellt
I am resolving the corresonding SQL11 bug as "by design". Here is the explanation: The semicolon between END TRY and BEGIN CATCH should not be allowed, because they are actually not different statements, but parts of the same TRY-CATCH statement. We only allow semicolons when they separate two statements in a sequence. A word of explanation why then we allow semicolons after BEGIN TRY and BEGIN CATCH. These keywords serve as opening "parentheses" that start an embedded statement sequence. Semicolons after BEGIN TRY/BEGIN CATCH get parsed as part of that embedded sequence, with the first statement in the sequence being empty. While we allow this syntax, I would not recommend it as a good coding practice because it creates a wrong impression of BEGIN TRY/BEGIN CATCH being independent, standalone statements.
Thanks Chris for this reference. It cleared the confusion in my mind.
June 11, 2015 at 1:35 am
I don't get the explanation.
The ; after the END TRY produced the error.
There is no statement between END TRY and BEGIn CATCH !
June 11, 2015 at 1:52 am
ArnoKwetters (6/11/2015)
I don't get the explanation.The ; after the END TRY produced the error.
There is no statement between END TRY and BEGIn CATCH !
The semicolon is a statement separator. So adding that after END TRY suggests that you end a statement and start a new one. (Albeit an "empty" statement - ;;; is valid syntax too!)
For the record, I do not like this syntax. BEGIN and END can be followed by semicolon, BEGIN TRY, BEGIN CATCH and END CATCH can be followed by semicolon, but END TRY cannot. To be consistent, I would like to either omit semicolon on *every* BEGIN and END (but there are cases where that is not allowed), or include it after *every* BEGIN and END (but END TRY is the exception here).
PS: You have been responding to lots of QotD topic lately. In most cases your posts are just a simple oneliner with no relevant content. You are of course free to do so, but in my opinion it doesn't add any value to the discussion. And it does increase the risk that people will simply ignore the SQLServerCentral "new post" notifications when they see your name, resulting in them also overlooking posts like this one, with a valid question, or posts where you do actually contribute new content to the discussion.
There are a few regulars over here who will post a bunch of "+1" or "me too" or "good question" to about every thread they find. They already are on my ignore list. I hope you decide not to join them.
Viewing 15 posts - 16 through 30 (of 31 total)
You must be logged in to reply to this topic. Login to reply