Very strange Application.ThreadException behaviour.
Posted
by Brann
on Stack Overflow
See other posts from Stack Overflow
or by Brann
Published on 2010-06-09T15:03:32Z
Indexed on
2010/06/09
15:12 UTC
Read the original article
Hit count: 284
I'm using the Application.ThreadException event to handle and log unexpected exceptions in my winforms application.
Now, somewhere in my application, I've got the following code (or rather something equivalent, but this dummy code is enough to reproduce my issue) :
try
{
throw new NullReferenceException("test");
}
catch (Exception ex)
{
throw new Exception("test2", ex);
}
I'm clearly expecting my Application_ThreadException handler to be passed the "test2" exception, but this is not always the case. Typically, if another thread marshals my code to the UI, my handler receives the "test" exception, exactly as if I hadn't caught "test" at all.
Here is a short sample reproducing this behavior. I have omitted the designer's code.
static class Program
{
[STAThread]
static void Main()
{
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
Console.WriteLine(e.Exception.Message);
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Click+=new EventHandler(button1_Click);
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThrowEx));
t.Start();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
throw new NullReferenceException("test");
}
catch (Exception ex)
{
throw new Exception("test2", ex);
}
}
void ThrowEx()
{
this.BeginInvoke(new EventHandler(button1_Click));
}
}
The output of this program on my computer is :
test
... here I click button1
test2
I've reproduced this on .net 2.0,3.5 and 4.0. Does someone have a logical explanation ?
© Stack Overflow or respective owner