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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:48:18+00:00 2026-05-27T14:48:18+00:00

I have a handler for Application.ThreadException , but I’m finding that exceptions aren’t always

  • 0

I have a handler for Application.ThreadException, but I’m finding that exceptions aren’t always getting passed to it correctly. Specifically, if I throw an exception-with-inner-exception from a BeginInvoke callback, my ThreadException handler doesn’t get the outer exception — it only gets the inner exception.

Example code:

public Form1()
{
    InitializeComponent();
    Application.ThreadException += (sender, e) =>
        MessageBox.Show(e.Exception.ToString());
}
private void button1_Click(object sender, EventArgs e)
{
    var inner = new Exception("Inner");
    var outer = new Exception("Outer", inner);
    //throw outer;
    BeginInvoke(new Action(() => { throw outer; }));
}

If I uncomment the throw outer; line and click the button, then the messagebox shows the outer exception (along with its inner exception):

System.Exception: Outer —> System.Exception: Inner
— End of inner exception stack trace —
at WindowsFormsApplication1.Form1.button1_Click(Object sender, EventArgs e) in C:\svn\trunk\Code Base\Source.NET\WindowsFormsApplication1\Form1.cs:line 55
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

But if the throw outer; is inside a BeginInvoke call, as in the above code, then the ThreadException handler only gets the inner exception. The outer exception gets stripped away before ThreadException is called, and all I get is:

System.Exception: Inner

(There’s no call stack here because inner never got thrown. In a more realistic example, where I caught one exception and wrapped it to re-throw, there would be a call stack.)

The same thing happens if I use SynchronizationContext.Current.Post instead of BeginInvoke: the outer exception is stripped off, and the ThreadException handler only gets the inner exception.

I tried wrapping more layers of exceptions around the outside, in case it was just stripping off the outermost exception, but it didn’t help: apparently somewhere there’s a loop doing something along the lines of while (e.InnerException != null) e = e.InnerException;.

I’m using BeginInvoke because I’ve got code that needs to throw an unhandled exception to be immediately handled by ThreadException, but this code is inside a catch block higher up the call stack (specifically, it’s inside the action for a Task, and Task will catch the exception and stop it from propagating). I’m trying to use BeginInvoke to delay the throw until the next time messages are processed in the message loop, when I’m no longer inside that catch. I’m not attached to the particular solution of BeginInvoke; I just want to throw an unhandled exception.

How can I cause an exception — including its inner exception — to reach ThreadException even when I’m inside somebody else’s catch-all?

(I can’t call my ThreadException-handler method directly, due to assembly dependencies: the handler is hooked by the EXE’s startup code, whereas my current problem is in a lower-layer DLL.)

  • 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-27T14:48:19+00:00Added an answer on May 27, 2026 at 2:48 pm

    One way to do it is to put the inner-exception reference in a custom property or the Data dictionary — i.e., leave the InnerException property null, and carry the reference some other way.

    Of course, this requires establishing some kind of convention that can be shared between the throwing code and the handling code. The best would probably be to define a custom exception class with a custom property, in a project that’s referenced by both pieces of code.

    Sample code (though it needs more comments to explain why it’s doing the crazy things it’s doing):

    public class ExceptionDecorator : Exception {
        public ExceptionDecorator(Exception exception) : base(exception.Message) {
            Exception = exception;
        }
        public Exception Exception { get; private set; }
    }
    
    // To throw an unhandled exception without losing its InnerException:
    BeginInvoke(new Action(() => { throw new ExceptionDecorator(outer); }));
    
    // In the ThreadException handler:
    private void OnUnhandledException(object sender, ThreadExceptionEventArgs e) {
        var exception = e.Exception;
        if (exception is ExceptionDecorator)
            exception = ((ExceptionDecorator) exception).Exception;
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that uses Ajax.Request and its onSuccess event handler in lots
I have a handler CsvExport.ashx that resides in the root of my web application,
I have an exception handler . In my asp.net application. It’s written in Global.asax.
In C++, a console application can have a message handler in its WinMain procedure,
I have a drag and drop handler in my Flex application. The drag proxy,
I have an action bean in my stripes application. The default handler/method will display
I have a PHP application that will on occasion have to handle URLs where
I have a Windows Forms application that uses some Application.Idle handlers to change the
I have an ASP.NET Web Application project that used to target ASP.NET 2.0. I
I have written a small ASHX handler that uses a small state object that

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.