why we use try catch in Sql server

  • hi all,

    I am using .net 2005 with sql server 2005.

    In this case why we have to use try catch in sql server?

    because all the exception occured in sql server will be catched by .net itself.so is there any exception that is catched only by sql server try catch block?

  • TRY/CATCH helps to write logic separate the action and error handling code in sql server.

    if some action fails in the try block you can roll back it in the catch block and also you can do error handling and raise the error.

    you can use the catch block to insert the error that occured to a table for analsys.

  • Adding to the above reply,

    The following system functions can be used to obtain information about the error that CATCH block to be executed

    ERROR_NUMBER() returns the number of the error.

    ERROR_SEVERITY() returns the severity.

    ERROR_STATE() returns the error state number.

    ERROR_PROCEDURE() returns the name of the stored procedure or trigger where the error occurred.

    ERROR_LINE() returns the line number inside the routine that caused the error.

    ERROR_MESSAGE() returns the complete text of the error message. The text includes the values supplied for any substitutable parameters, such as lengths, object names, or times.

    If these functions are used outside the CATCH block, then all of them will return NULL value.

  • THANK YOU VERY MUCH

  • Begin try

    Begin transaction

    --------

    --------

    Commit transaction

    End try

    Begin catch

    Rollback transaction

    End catch

    http://planetofcoders.blogspot.com/2011/06/how-we-can-use-try-catch-in-sql.html

Viewing 6 posts - 1 through 5 (of 5 total)

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