Try/Catch RaisError Question

  • I am doing something simple using Try/Catch Blocks that I need to confirm. If I execute a RaisError in the middle of a Try/Catch block will the code jump immediately to my catch block. For instance in the following pseudo code will the inserts be skipped and an error raised if 'SomeCondition' is true?

    Begin Try

    Begin Transaction

    If SomeCondition

    Begin

    RaisError("I hope the Transaction Rolls back", 16,1)

    End

    Insert Something

    Insert Something

    Commit Transaction

    End Try

    Begin Catch

    If @@TranCount > 0

    Begin

    RollBack Transaction

    End

    RaisError("Opps", 16, 1)

    End Catch

    Thanks

  • It depends on the severity that you set. From BOL:

    A RAISERROR severity of 11 to 19 executed in the TRY block of a TRY…CATCH construct causes control to transfer to the associated CATCH block. Specify a severity of 10 or lower to return messages using RAISERROR without invoking a CATCH block.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Thanks so much for clarifying this.

  • Glad I could help.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Hi problem with raiserror

    select 1

    no problem ouput will come 1

    If i write like below

    select 1

    --raiserror

    Msg 121, Level 20, State 0, Line 0

    A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0 - The semaphore timeout period has expired.)

    it show this message. if commented of RAISERROR

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

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