469,898 Members | 1,832 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

Catching unhandled exception from within a thread

Hi,

This is my Program class in my Application -

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);

// Subscribe to thread (unhandled) exception events
ThreadExceptionHandler handler = new
ThreadExceptionHandler();
Application.ThreadException += new
ThreadExceptionEventHandler(handler.ApplicationThr eadException);

Application.Run(new FormMain());
}

/// <summary>
/// Handles any thread exceptions
/// </summary>
public class ThreadExceptionHandler
{
// set when there is an unhandled exception. Can therefore
be used by FormMain.
static bool unhandeledException = false;
public static bool UnhandeledException { get { return
unhandeledException; } }

public void ApplicationThreadException(object sender,
ThreadExceptionEventArgs e)
{
try
{
unhandeledException = true;
ShowThreadExceptionDialog(e.Exception);
}
finally
{
// Fatal error, terminate program
Application.Exit();
}
}

/// <summary>
/// Creates and displays the error message.
/// </summary>
private void ShowThreadExceptionDialog(Exception ex)
{
from with FormMain I do the following if there is an exception which
is to not save the users settings -

private void OnClosing(object sender, EventArgs ev)
{
if (Program.ThreadExceptionHandler.UnhandeledExceptio n)
return;

// save user settings...
}

The problem though is that if I create a thread as follows, exceptions
are not being handled by ThreadExceptionHandler -

public void FormMain_Load(object sender, EventArgs e)
{
thread = new System.Threading.Thread(MyThread);

thread.Start();
}

void MyThread()
{
throw new ApplicationException("My exception");
}

Why not?

Thanks as ever,

Aine

Nov 13 '07 #1
1 1688
On 2007-11-13 01:27:47 -0800, ai********@yahoo.com said:
[...]
The problem though is that if I create a thread as follows, exceptions
are not being handled by ThreadExceptionHandler -

[...]
Why not?
Just to be clear: the Application.ThreadException event doesn't offer a
way to _handle_ an exception. You'll be notified of the exception, but
handling implies that you've successfully caught it and the thread can
proceed normally.

As for why your event handler isn't being called, I don't really know.
Have you called Application.SetUnhandledExceptionMode to enable the
event? I'm not sure what the configuration file default is, but it
could be that it's simply set currently to not raise the
ThreadException event.

Pete

Nov 13 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Hubert Hermanutz | last post: by
5 posts views Thread by Ron James | last post: by
5 posts views Thread by Lucvdv | last post: by
reply views Thread by Colmeister | last post: by
5 posts views Thread by Simon Tamman {Uchiha Jax} | last post: by
5 posts views Thread by Simon Tamman | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.