• Use TRY and CATCH

    BEGIN TRY

    -- Table does not exist; object name resolution

    -- error not caught.

    SELECT * FROM NonexistentTable;

    END TRY

    BEGIN CATCH

    SELECT

    ERROR_NUMBER() AS ErrorNumber

    ,ERROR_MESSAGE() AS ErrorMessage;

    END CATCH

    check BOL

    http://technet.microsoft.com/en-us/library/ms130214.aspx

    SQL 2000/2005/2008/2012 DBA - MCTS/MCITP