Error Handling using TRY and CATCH statements

Basic Example of error handling using TRY and CATCH statement. In this example i am trying to store a varchar value to an int variable which cause error:



BEGIN TRY
-- trying to store a varchar value to an int variable to raise error, to be catched in CATCH statement
    DECLARE @propertyID INT;
    DECLARE @propertyName VARCHAR(50);

    SET @propertyName = 'ABC';
    SET @propertyID = @propertyName;


    SELECT  @propertyID;

END TRY


BEGIN CATCH

    SELECT  ERROR_LINE() AS ErrorLineNumber ,
            ERROR_MESSAGE() AS ErrorMessage ,
            ERROR_NUMBER() AS ErrorNumber ,
            ERROR_PROCEDURE() AS ErrorProcName ,
            ERROR_SEVERITY() AS ErrorSeverity ,
            ERROR_STATE() AS ErrorState;

END CATCH;

Comments

Popular posts from this blog

TSQL To Get All the Names of tables and Databases whose data have changed in last 24 Hours

To Get All the Names of tables and Databases their data have changed in last 24 Hours And Backup those databases

Apply paging in procedure