Error handling not working with Stored proc

Answered Error handling not working with Stored proc

  • Friday, January 18, 2013 1:15 AM
     
     

    Hi, i have a syntax of proc as,

    CREATE PROC procname

    Begin

    Begin TRY

    ---------code----------

    End TRY

    Begin CATCH

    EXEC dbo.ErrorHandling

    End CATCH

    END

    Syntax is correct but Error Handling proc doesnt load the error table

    while if i simply test this, It works and loads error table.

    BEGIN TRY

    SELECT 1/0

    END TRY

    BEGIN CATCH

    EXEC dbo.spErrorHandling

    END CATCH

    Any idea why its not working with the procedure?

All Replies

  • Friday, January 18, 2013 1:39 AM
    Moderator
     
     
    It depends on the procedure code. Do you know if that error is trappable? Did you open a transaction inside the procedure? If so, that procedure call will participate in the transaction and if you rollback the main transaction, the rows will not be written to the error table.

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

  • Friday, January 18, 2013 1:42 AM
     
     

    See this for the details

    http://msdn.microsoft.com/en-us/library/ms179296%28v=SQL.105%29.aspx


    Many Thanks & Best Regards, Hua Min

  • Friday, January 18, 2013 4:57 AM
     
     

    Hello Skydiver01,

    Please have a look at TRY...CATCH (Transact-SQL) => Remarks "A TRY…CATCH construct catches all execution errors that have a severity higher than 10 that do not close the database connection."

    So it depends on the raised error, if try/catch works or not. See also Using TRY...CATCH in Transact-SQL for further informations.


    Olaf Helper

    Blog Xing

  • Friday, January 18, 2013 9:31 AM
     
     

    Exactly what error is not trapped? If it's a missing table, that error cannot be trapped in the procedure where it occurs, but only in outer scopes.


    Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
  • Friday, January 18, 2013 5:36 PM
     
     

    @Naomi, How do i find out if the error is trappable? The error i am getting is like, 

    Msg 208, Level 16, State 1, Procedure uspCustomerUpdate, Line 46
    Invalid object name 'CustomerDetails'.

    I have another proc which is having trasaction. How do i log errors in that proc?

    @Olaf Helper, How do i know the severity since the proc doesnt reach to catch block.



    • Edited by Skydiver01 Friday, January 18, 2013 5:37 PM
    •  
  • Friday, January 18, 2013 5:53 PM
     
     

    @Olaf Helper, How do i know the severity since the proc doesnt reach to catch block?


    When you execute the statement in SSMS, then it reports all uncatched errors in the "Messages" tab under the query editor window.

    Olaf Helper

    Blog Xing

  • Friday, January 18, 2013 6:59 PM
     
     

    You might find some more information about your code execution by checking XACT_STATE().

    http://msdn.microsoft.com/en-us/library/ms189797.aspx

    Also read:

    http://msdn.microsoft.com/en-us/library/aa175920(v=sql.80).aspx

  • Friday, January 18, 2013 7:20 PM
    Moderator
     
     

    Do you get this error in run-time or when you try to save the procedure script? It looks like you're getting the deferred name resolution error which is, as far as I know, non-trappable.

    Do you want to share the procedure code? 

    Or at least let us know if the error you get in run-time or design time and which line of code it is.


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

  • Friday, January 18, 2013 9:38 PM
     
     Answered

    @Naomi, How do i find out if the error is trappable? The error i am getting is like, Msg 208, Level 16, State 1, Procedure uspCustomerUpdate, Line 46 Invalid object name 'CustomerDetails'.

    This is exactly the kind of error that I talked about in my post. Compilations that occurs at run-time like a missing table and others related to deferred name resolution are not trappable in the scope they occur. They are trappable in outer scopes. Consider this:

    CREATE PROCEDURE inner_sp AS
    BEGIN TRY
         PRINT 'This prints'
         SELECT * FROM nosuchtable
    END TRY
    BEGIN CATCH
         PRINT 'This does not print'
    END CATCH
    go
    CREATE PROCEDURE outer_sp aS
    BEGIN TRY
         EXEC inner_sp
    END TRY
    BEGIN CATCH
         PRINT 'The error is: ' + error_message()
    END CATCH
    go
    EXEC outer_sp
    go
    DROP PROCEDURE inner_sp, outer_sp

    Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se