THROW won't continue a while loop

  • I decided to try THROW instead of RAISERROR but it doesn't seem to be designed to allow something like a while loop to continue after the CATCH block.

    DECLARE @ErrMsg NVARCHAR(4000)
        ,@ErrSeverity INT
    declare @counter int
    set @counter=5
    while @counter>0
    begin
    BEGIN TRY
    set @counter=@counter-1
    SELECT 4/0

    END TRY

    BEGIN CATCH
    print @counter
    -------------------------;THROW

    -- There was an error    No transaction here, just test code
            IF @@TRANCOUNT > 0
                ROLLBACK;
    -- Raise an error with the details of the exception
                SELECT @ErrMsg = ERROR_MESSAGE()
                ,@ErrSeverity = ERROR_SEVERITY();

            RAISERROR (
                    @ErrMsg
                    ,@ErrSeverity
                    ,1
                    );

                    -- do something important here before the next Loop
    END CATCH

    end

  • Leave off the Raiserror function and maybe add logging to a table instead.

  • Your code works for me.

    Here isd what I see on Messages ta

    (0 row(s) affected)
    4
    Msg 50000, Level 16, State 1, Line 24
    Divide by zero error encountered.

    (0 row(s) affected)
    3
    Msg 50000, Level 16, State 1, Line 24
    Divide by zero error encountered.

    (0 row(s) affected)
    2
    Msg 50000, Level 16, State 1, Line 24
    Divide by zero error encountered.

    (0 row(s) affected)
    1
    Msg 50000, Level 16, State 1, Line 24
    Divide by zero error encountered.

    (0 row(s) affected)
    0
    Msg 50000, Level 16, State 1, Line 24
    Divide by zero error encountered.

    All 5 cycles have been executed.

    Did you check ARITHABORT settings?

    _____________
    Code for TallyGenerator

  • If you comment out the RaisError portion and try to use THROW instead, it stops after one loop.  I'd like to use THROW and have it continue the while  loop.

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

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