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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:45:36+00:00 2026-05-27T13:45:36+00:00

So I followed the recommended answer shown at How to rethrow the inner exception

  • 0

So I followed the recommended answer shown at How to rethrow the inner exception of a TargetInvocationException without losing the stack trace and ended up with code that looks like this:

// Inside DpmEntrypoint static class.
public static object Invoke(Delegate d, object[] args)
{
    try
    {
        // Invoke directly if not networked.
        if (LocalNode.Singleton == null)
            return d.DynamicInvoke(args);

        // Get the network name of the object and the name of the method.
        string objectName = (d.Target as ITransparent).NetworkName;
        string methodName = d.Method.Name;

        // Get our local node and invoke the method.
        return LocalNode.Singleton.Invoke(objectName, methodName, args);
   }
   catch (Exception ex)
   {
        ex.Rethrow();
        return null;
    }
}

// Inside ExceptionExtensions static class.
public static void Rethrow(this Exception ex)
{
    if (ex is TargetInvocationException)
        ex.InnerException.Rethrow();
    else
    {
        typeof(Exception).GetMethod("PrepForRemoting",
            BindingFlags.NonPublic | BindingFlags.Instance)
            .Invoke(ex, new object[0]);
        throw ex;
    }
}

In this case, a post-processor is run over assemblies after compile time and specified methods are wrapped in a delegate and invoked using the method above. In order to then call the delegate, I have to use Invoke somewhere along the line (even if it’s in the reflection used inside Singleton.Invoke).

The Rethrow method above correctly preserves the stack trace like so:

Server stack trace:
at Example.ExampleController.NullTest() in C:\Server Storage\Projects\Redpoint\Pivot\Example\ExampleController.cs:line 19
at Example.ExampleWorld.t_Spawned(Object sender, EventArgs e) in C:\Server Storage\Projects\Redpoint\Pivot\Example\ExampleWorld.cs:line 29
at Pivot.Core.Actor.OnSpawned__Distributed0() in C:\Server Storage\Projects\Redpoint\Pivot\Pivot.Core\Actor.cs:line 62

Exception rethrown at [0]:
at Process4.Providers.ExceptionExtensions.Rethrow(Exception ex)
at Process4.Providers.ExceptionExtensions.Rethrow(Exception ex)
at Process4.Providers.DpmEntrypoint.Invoke(Delegate d, Object[] args)
at Pivot.Core.Actor.OnSpawned()
at Pivot.Core.GameInfo.set_World__Distributed0(WorldInfo value) in C:\Server Storage\Projects\Redpoint\Pivot\Pivot.Core\GameInfo.cs:line 35

Exception rethrown at 1:
at Process4.Providers.ExceptionExtensions.Rethrow(Exception ex)
at Process4.Providers.ExceptionExtensions.Rethrow(Exception ex)
at Process4.Providers.DpmEntrypoint.SetProperty(Delegate d, Object[] args)
at Pivot.Core.GameInfo.set_World(WorldInfo value)
at Example.ExampleGame.t_Spawned(Object sender, EventArgs e) in C:\Server Storage\Projects\Redpoint\Pivot\Example\ExampleGame.cs:line 25
at Pivot.Core.Actor.OnSpawned__Distributed0() in C:\Server Storage\Projects\Redpoint\Pivot\Pivot.Core\Actor.cs:line 62

Exception rethrown at [2]:
at Process4.Providers.ExceptionExtensions.Rethrow(Exception ex)
at Process4.Providers.ExceptionExtensions.Rethrow(Exception ex)
at Process4.Providers.DpmEntrypoint.Invoke(Delegate d, Object[] args)
at Pivot.Core.Actor.OnSpawned()
at Pivot.Engine.set_Game(GameInfo value) in C:\Server Storage\Projects\Redpoint\Pivot\Pivot.Core\Engine.cs:line 47
at Example.Program.Main(String[] args) in C:\Server Storage\Projects\Redpoint\Pivot\Example\Program.cs:line 14
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

but the problem I have is that even when the library is compiled in Release mode, Visual Studio still shows the exception source at Rethrow instead of NullTest (where the NullReferenceException is thrown).

Since all methods in an application will be hooked, throwing the TargetInvocationException as it is is useless to the developer; they’re more interested in where the original exception occurred in their code.

Without the ability to rethrow the inner exception as it is, it basically makes the entire exception system in .NET when used with the Distributed Processing Library without the developer being aware of what’s going on behind the scenes (which the exact opposite goal of the library).

Does anyone know a way of causing Visual Studio to show it at the location it was originally thrown?

EDIT:

I’m thinking of solving the issue using C++/CLI since that gives me access some special functionality as described in the MSDN article Transporting Exceptions Between Threads.

Problem is that those functions don’t let me “transport managed exceptions” and hence rethrow_exception throws an SEHException when I try to use it on a managed exception. If anyone knows a way around this issue then it can be solved using C++/CLI (if only there was a way to get the exact pointer of the current exception then the rethrow IL instruction could be used!).

namespace Rethrow {
    [System::Runtime::CompilerServices::Extension]
    public ref class ExceptionExtensions abstract sealed
    {
    public: 
        [System::Runtime::CompilerServices::Extension]
        static void Rethrow(System::Exception^ s)
        {
            std::rethrow_exception(std::copy_exception<System::Exception^>(s));
            throw;
        }
    };
}
  • 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-27T13:45:37+00:00Added an answer on May 27, 2026 at 1:45 pm

    I managed to solve this issue by modifying the post-processor to invoke the method directly via a matching delegate (since the post-processor knows at runtime method signatures and can generate appropriate matching delegates). When using Invoke() on a delegate it won’t wrap any exceptions with TargetInvocationException; that only occurs when dynamically invoking a method.

    Unfortunately this solution doesn’t actually solve dynamically invoking methods and passing inner exceptions; it just takes advantage of the post-processor that already exists in the build process for this particular library (i.e. this solution doesn’t solve the problem for anyone building a library that doesn’t do post-processing anyway).

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

Sidebar

Related Questions

The first topic is What wrong with my InvokeRequied I followed the answer that
Someone recommended I use code completion and I realized that while my IDE has
I notice that the official recommended book The Definitive Guide to Django was written
I followed this guide which I was recommended , but I can't get it
Is there a recommended process for creating reusable ASP.NET assemblies that contain UserControls that
I noticed that System.Threading.Thread implements a finalizer but not IDisposable. The recommended practice is
Admob code can't see on device. I followed the instructions here: http://www.admob.com/docs/AdMob_Android_SDK_Instructions.pdf everything ok,
I followed this tutorial and got problem that certificate has expired. I looked up
Having followed a tutorial I have a hashtable that contains a TcpClient object that
Followed this question about delayed_job and monit Its working on my development machine. But

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.