Below is a snippet of my
TRY/CATCH logic within a stored procedure with error handling (I did not include all of the
DECLARE statements). This is being ran within a stored procedure, however I am simply testing this in a session via SSMS (would this affect the behavior?). I did stumble across this thread, but do I have to go to these lengths to simply capture errors?
BEGIN TRY DECLARE @sql varchar(1000); SET @sql = 'select 1/0'; EXEC(@sql); -- On Error the remaining TRY/CATCH below is compeltely ignored I discovered. DECLARE @error int; SET @error = @@error; IF @error > 0 BEGIN SET @raisemessage = 'SQL Backup Error: ' + cast(@rc as varchar(10)); RAISERROR (@raisemessage, 16,1); END END TRY BEGIN CATCH SET @errormessage = ERROR_MESSAGE(); IF @errormessage is null SET @errormessage = '' SET @errorstring = @errorstring + 'Database: ' + @name + ' Error: ' + @errormessage + char(10) + char(10); END CATCH
Can anyone explain the shortcomings of this TRY/CATCH block? Does this not work in T-SQL?