Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 558511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:08:24+00:00 2026-05-13T12:08:24+00:00

This is a follow up to the question Nested stored procedures containing TRY CATCH

  • 0

This is a follow up to the question
Nested stored procedures containing TRY CATCH ROLLBACK pattern?

In the catch block I use a stored procedure to report (reraise) the error by reading from ERROR_MESSAGE(), ERROR_PROCEDURE(), ERROR_LINE(), etc. As described here I also have a check so that it can determine if the error has already been rethrown (this happens with nested stored procedures as the error information is passed down through each TRY CATCH block).

What I would like to do, either directly in ‘ReportError’, or indirectly with my pattern (as described in the first question), is record a stack trace – so when ReportError detects that it is receving an error thrown by itself, it appends the next level of the stack to the error message. This would help me avoid cases where I see an error message coming from some little utility stored procedure, without any way of knowing what called it. If I try doing this directly in ReportError it fails, since the rethrown error reports itself as coming from ReportError – only the original error is visible.

Is there some way for ReportError to perform a stack trace in SQL Server, without passing an argument to every single stored procedure, and without manually maintaining such a trace with #temp table? Basically I want a recursive call of ERROR_PROCEDURE() and ERROR_LINE().

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:08:24+00:00Added an answer on May 13, 2026 at 12:08 pm

    Ok, I’ll add our error handling back in 🙂

    The ERROR_%() functions are visible to the scope of the CATCH block. This means you can use them in a stored proc or function call in each CATCH block

    And with nested stored procs, it’s useful to know what caused the error and what’s logging the error

    ...
    END TRY
    BEGIN CATCH
        IF XACT_STATE() <> 0 AND @starttrancount = 0 
            ROLLBACK TRANSACTION
        EXEC dbo.MyExceptionHandler @@PROCID, @errmsg OUTPUT;
        RAISERROR (@errmsg, 16, 1);
    END CATCH
    
    ---with this handler (cut down version of ours)
    CREATE PROCEDURE dbo.MyExceptionHandler
        @CallerProcID int,
        @ErrorMessage varchar(2000) OUTPUT
    WITH EXECUTE AS OWNER --may be needed to get around metadata visibility issues of OBJECT_NAME
    AS
    SET NOCOUNT, XACT_ABORT ON;
    
    BEGIN TRY
        SET @ErrorMessage = --cutdown
                CASE
                    WHEN @errproc = @callerproc THEN        --Caller = error generator
                            --build up stuff
    
                    ELSE    --Just append stuff             --Nested error stack
                END;
    
        IF @@TRANCOUNT = 0
            INSERT dbo.Exception (Who, TheError, WhatBy, LoggedBy)
            VALUES (ORIGINAL_LOGIN()), RTRIM(ERROR_MESSAGE()), ERROR_PROCEDURE(), OBJECT_NAME(@CallerProcID));
    END TRY
    BEGIN CATCH
       --and do what exactly?
    END CATCH
    GO
    

    This is the basic idea anyway: each CATCH block is simple, the work goes on in the error handler. Eg append ERROR_NUMBER() if you want to

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.