1

I'm having a problem with TRY...CATCH blocks. Can someone explain why the following code will not execute my sp?

DECLARE @Result int
SET @Result = 0
BEGIN TRY
    SELECT * FROM TableNoExist
END TRY
BEGIN CATCH
    SET @Result = ERROR_NUMBER()
END CATCH
EXEC dbo.spSecurityEventAdd @pSecurityEventTypeID = 11, @pResult = @Result

But this code does work:

DECLARE @Result int
SET @Result = 0
BEGIN TRY
    SELECT 1/0
END TRY
BEGIN CATCH
    SET @Result = ERROR_NUMBER()
END CATCH
EXEC dbo.spSecurityEventAdd @pSecurityEventTypeID = 11, @pResult = @Result

I'd like to make sure I catch all errors. Thanks

2 Answers 2

2

Compile and Statement-level Recompile Errors

There are two types of errors that will not be handled by TRY…CATCH if the error occurs in the same execution level as the TRY…CATCH construct:

Compile errors, such as syntax errors that prevent a batch from executing.

Errors that occur during statement-level recompilation, such as object name resolution errors that happen after compilation due to deferred name resolution.

http://msdn.microsoft.com/en-us/library/ms179296.aspx

Sign up to request clarification or add additional context in comments.

Comments

1

It looks like this thread answers your question.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.