TRY ... CATCH problem

  • The following error (referring a non-existent column) is not catched:

    begin try

    insert into excludednames(doesnotexist, metadata) values('selectednames',1)

    end try

    begin catch

    print 'Gotcha!'

    print ERROR_MESSAGE()

    end catch

    This results in a severity 16, which should be handled by the catch block, but it isn't

    Why? Am I missing something?

    SQL 2005 EE x86 (9.00.3282)

    Wilfred
    The best things in life are the simple things

  • 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.

    When the batch, stored procedure, or trigger that contains the TRY…CATCH construct generates one of these errors, the TRY…CATCH construct does not handle these errors. These errors will return to the application or batch that called the error-generating routine.

  • Wilfred van Dijk (11/20/2008)


    The following error (referring a non-existent column) is not catched:

    begin try

    insert into excludednames(doesnotexist, metadata) values('selectednames',1)

    end try

    begin catch

    print 'Gotcha!'

    print ERROR_MESSAGE()

    end catch

    This results in a severity 16, which should be handled by the catch block, but it isn't

    Why? Am I missing something?

    SQL 2005 EE x86 (9.00.3282)

    you can see the more info on SQL BOL.

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

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