I apologize is this is a repeat. I couldn't find a question that was answered that fit what I was asking. (this is similar How can i create out parameters with RAISERROR in stored procedure?)
I have a very simple job in SQL Server Agent with one step. The package only executes a stored procedure. The stored procedure has a try / catch block and when I try to cause an exception it is caught but doesn't propagate up and cause the job to report an error. The job succeeds. I log the error into a local table and commit it so I know I am indeed entering the catch block. I have tried raising error with severity levels 16 and 20 and some other things that were probably not worth mentioning.
Is this a package setting that would cause it to not propagate or is a problem with my stored procedure? Thanks.
-- dumbed down structure
BEGIN
SET NOCOUNT ON;
-- buncha vars
BEGIN TRY
BEGIN TRANSACTION
set @StepNumber = 10;
select 1/0;
COMMIT TRANSACTION;
RETURN 0;
END TRY
BEGIN CATCH
-- get the error
SELECT @ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE();
-- rollback the transaction
ROLLBACK TRANSACTION;
-- log the caught exception into the local log
set @LogTimestamp = GETUTCDATE();
set @LogMessage = @SessionGUID + ' - ' + CAST(@StepNumber AS NVARCHAR) + ' - ' + ' ERROR - ENTERED CATCH BLOCK - ERROR_MESSAGE()=' + @ErrorMessage + ' ERROR_SEVERITY()=' + cast(@ErrorSeverity as nvarchar) + ' ERROR_STATE()=' + CAST(@ErrorState as nvarchar) ;
insert into @LocalLog(TimestampDT, SourceVch, MessageVch) values (@LogTimestamp, @LogSource, @LogMessage);
-- transfer local log to master log table
BEGIN TRANSACTION;
--insert into RealTable selecting from @LocalLog;
COMMIT TRANSACTION;
-- slightly modify the error message before raising it to parent process
SET @ErrorMessage = @LogSource + ' - error with @StepNumber=[' +CAST(@StepNumber as nvarchar)+ '] ERROR_MESSAGE()=' + @ErrorMessage;
-- rethrow the error to the calling process
RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState);
-- do we need this return?
RETURN @StepNumber;
END CATCH
END
The log table I get tells me I caught the error at least (From my log table). "6a3e80fd-f480-459c-a42f-25fd3d5a42a8 - 125 - ERROR - ENTERED CATCH BLOCK - ERROR_MESSAGE()=Divide by zero error encountered. ERROR_SEVERITY()=16 ERROR_STATE()=1"