• Generally speaking using try/catch is preferred to examining @@error.

    Using try/catch will successfully trap the runtime errors encountered for items 1, 2 and 3.

    begin try

    insert TESTTABLE

    select 1, 'Text1', 1

    end try

    begin catch

    print 'error occured'

    end catch

    begin try

    insert TESTTABLE

    select 2, 'Text2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2

    end try

    begin catch

    print 'error occured'

    end catch

    begin try

    insert TESTTABLE

    select 3, 'Text3', 'Text3'

    end try

    begin catch

    print 'error occured'

    end catch

    However, the error in #4 is going to throw a compile error. That means it will parse fine but the exception is thrown when the engine compiles your statement. That means it can't be caught because it actually happens prior to executing.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/