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 553645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:37:37+00:00 2026-05-13T11:37:37+00:00

Duplicate of: In C#, how can I rethrow InnerException without losing stack trace? I

  • 0

Duplicate of: In C#, how can I rethrow InnerException without losing stack trace?

I have some operations that I invoke asynchronously on a background thread. Sometimes, things go bad. When this happens, I tend to get a TargetInvocationException, which, while appropriate, is quite useless. What I really need is the TargetInvocationException’s InnerException, like this:

    try
    {
        ReturnValue = myFunctionCall.Invoke(Target, Parameters);
    }
    catch (TargetInvocationException err)
    {
        throw err.InnerException;
    }

That way, my callers are served up with the REAL exception that occured. The problem is, that the throw statement seems to reset the stack trace. I’d like to basically rethrow the inner exception, but keep the stack trace it originally had. How do I do that?

CLARIFICATION:
The reason I want only the inner exception is that this class tries to ‘abstract away’ the whole fact that these functions (delegates supplied by caller) are run on other threads and whatnot. If there is an exception, then odds are it has nothing to do with being run on a background thread, and the caller would really like the stack trace that goes into their delegate and finds the real issue, not my call to invoke.

  • 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-13T11:37:38+00:00Added an answer on May 13, 2026 at 11:37 am

    It is possible to preserve the stack trace before rethrowing without reflection:

    static void PreserveStackTrace (Exception e)
    {
        var ctx = new StreamingContext  (StreamingContextStates.CrossAppDomain) ;
        var mgr = new ObjectManager     (null, ctx) ;
        var si  = new SerializationInfo (e.GetType (), new FormatterConverter ()) ;
    
        e.GetObjectData    (si, ctx)  ;
        mgr.RegisterObject (e, 1, si) ; // prepare for SetObjectData
        mgr.DoFixups       ()         ; // ObjectManager calls SetObjectData
    
        // voila, e is unmodified save for _remoteStackTraceString
    }
    

    This wastes a lot of cycles compared to InternalPreserveStackTrace, but has the advantage of relying only on public functionality. Here are a couple of common usage patterns for stack-trace preserving functions:

    // usage (A): cross-thread invoke, messaging, custom task schedulers etc.
    catch (Exception e)
    {
        PreserveStackTrace (e) ;
    
        // store exception to be re-thrown later,
        // possibly in a different thread
        operationResult.Exception = e ;
    }
    
    // usage (B): after calling MethodInfo.Invoke() and the like
    catch (TargetInvocationException tiex)
    {
        PreserveStackTrace (tiex.InnerException) ;
    
        // unwrap TargetInvocationException, so that typed catch clauses 
        // in library/3rd-party code can work correctly;
        // new stack trace is appended to existing one
        throw tiex.InnerException ;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How can I find the method that called the current method? I
Closed as exact duplicate of How can I find the method that called the
Possible Duplicate: Can a JavaScript object have a prototype chain, but also be a
Possible Duplicate: How can I declare a thousand separator in read.csv? I actually have
Possible Duplicate: Can’t operator == be applied to generic types in C#? I've got
Possible Duplicate: Can’t operator == be applied to generic types in C#? I've coded
Possible Duplicate: How can I use a carriage return in a HTML tooltip? I'd
Possible Duplicate: How can I convert my java program to an .exe file ?
Possible Duplicate: How can I pre-set arguments in JavaScript function call? (Partial Function Application)
How can I remove duplicate values from an array in PHP?

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.