TRY...CATCH

  • Comments posted to this topic are about the item TRY...CATCH

    ___________________________________________________________________
    If I can answer a question then anyone can answer it..trying to reverse the logic.. :hehe:

  • Good one. New thing come to know.

    Thank. Keep it up !!! 🙂

    Thanks
    Vinay Kumar
    -----------------------------------------------------------------
    Keep Learning - Keep Growing !!!

  • Good question :-):-)

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Nice....

  • 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).


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • 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".

  • This was removed by the editor as SPAM

  • 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 for clearing that up Hugo - I was scratching my head trying to work out where the extra statement between the try and the catch was.

  • +1... nice question to learn about TRY-Catch....

    Learn new things every where

    Manik

    Manik
    You cannot get to the top by sitting on your bottom.

  • 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).

    +1... nice....

    Manik
    You cannot get to the top by sitting on your bottom.

  • Definately learned something about the odd behaviour of SQL Server...

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Stewart "Arturius" Campbell (4/2/2013)


    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).

    +1

    what a gotcha...

    +1. The thought of this weird behavior happening never even crossed my mind. Great question.



    Everything is awesome!

  • 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).

    +1..

    Thanks Hugo for the explanation and helping me out on this one. This is my first Qotd and I had this whole zero length statement explanation set up but for some reason got mixed up and posted the condensed version. As usual you are there to help out all the newbies like me.. Your posts are always very insightful..

    Go Hugo !!! 😀

    ___________________________________________________________________
    If I can answer a question then anyone can answer it..trying to reverse the logic.. :hehe:

  • Good question, but I agree that SQL is not easy when it comes to the use of the semi-colon, compared to C#.

    Thanks,

    Lon

  • 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".

    That is the same reason I saw the problem with the code. THROW on 2005,2008 and R2 will fail with a syntax error as well.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

Viewing 15 posts - 1 through 15 (of 31 total)

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