locked
Try ... Catch RRS feed

  • Question

  • Why doesn't the following procedure go into the catch block?

     

    create procedure usp_test
    as
    
    begin try
     create table #t_test (col_1 int)
     select col_2 from #t_test -- There is no col_2 in the #t_test
    end try
    
    begin catch
     select
     ERROR_NUMBER() as ErrorNumber,
     ERROR_SEVERITY() as ErrorSeverity,
     ERROR_STATE() as ErrorState,
     ERROR_PROCEDURE() as ErrorProcedure,
     ERROR_LINE() as ErrorLine,
     ERROR_MESSAGE() as ErrorMessage
    end catch
    
    exec usp_test

    Wednesday, March 2, 2011 9:42 PM

Answers

  • The error caused by the invalid column, bubbles up to the next level.

    so if we wrap around the stored procedure call:

    begin try
    exec usp_test
    end try
    begin catch
     select ERROR_MESSAGE()
    end catch
    
    
    Invalid column name 'col_2'.
    Jon
    • Proposed as answer by Naomi N Wednesday, March 2, 2011 11:36 PM
    • Marked as answer by KJian_ Tuesday, March 8, 2011 8:49 AM
    Wednesday, March 2, 2011 10:56 PM

All replies

  • Probably this will has to return an "Invalid Column Name" error. It will not go to your catch portion. It is a syntax error.

    Check try and Catch in here:

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



    Best Wishes, Arbi --- MCC 2011; Please vote if you find this posting was helpful or Mark it as answered.
    • Proposed as answer by Naomi N Wednesday, March 2, 2011 10:38 PM
    Wednesday, March 2, 2011 9:52 PM
  • Google on 'deferred name resolution'. Also, I think Erland explains it in his article about error handling and BOL explains it as well. Not every error is trappable and bad field names/table names errors are not trappable by error handler.
    For every expert, there is an equal and opposite expert. - Becker's Law

    Naomi Nosonovsky, Sr. Programmer-Analyst

    My blog
    Wednesday, March 2, 2011 10:40 PM
  • The error caused by the invalid column, bubbles up to the next level.

    so if we wrap around the stored procedure call:

    begin try
    exec usp_test
    end try
    begin catch
     select ERROR_MESSAGE()
    end catch
    
    
    Invalid column name 'col_2'.
    Jon
    • Proposed as answer by Naomi N Wednesday, March 2, 2011 11:36 PM
    • Marked as answer by KJian_ Tuesday, March 8, 2011 8:49 AM
    Wednesday, March 2, 2011 10:56 PM
  • Yes, that is the error.
    Wednesday, March 2, 2011 11:23 PM