Understanding RAISERROR with TRY CATCH

  • This script allows you to quickly understand and demonstrate how a TRY CATCH block will execute with RAISERROR.

    DECLARE @level int = 11 --Test using 1, 10, 11, 18 & 20

    PRINT 'Begin'

    BEGIN TRY

    PRINT ' Try'

    RAISERROR (' Error level %d raised.', @level, 1, @level) WITH NOWAIT

    PRINT ' Continue without handling.'

    END TRY

    BEGIN CATCH

    PRINT ' Catch and handle.'

    PRINT REPLACE(' {0}', '{0}', ERROR_MESSAGE())

    PRINT REPLACE(' Error Severity:', '{0}', ERROR_SEVERITY())

    PRINT REPLACE(' Error #{0}', '{0}', ERROR_NUMBER())

    END CATCH

    PRINT 'Done'

Viewing 0 posts

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