SQL Try catch purpose unclear

Posted by PaN1C_Showt1Me on Stack Overflow See other posts from Stack Overflow or by PaN1C_Showt1Me
Published on 2010-06-10T13:10:09Z Indexed on 2010/06/10 13:12 UTC
Read the original article Hit count: 291

Filed under:
|

Let's suppose I want to inform the application about what happened / returned the SQL server. Let's have this code block:

BEGIN TRY
    -- Generate divide-by-zero error.
    SELECT 1/0;
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;
GO

and Let's have this code block:

SELECT 1/0;

My question is: Both return the division by zero error. What I don't understand clearly is that why I should surround it with the try catch clausule when I got that error in both cases ? Isn't it true that this error will be in both cases propagated to the client application ?

© Stack Overflow or respective owner

Related posts about sql

Related posts about try-catch