Catching unhandled exception from within a thread

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aine_canby@yahoo.com

    #1

    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.Ena bleVisualStyles ();
    Application.Set CompatibleTextR enderingDefault (false);

    // Subscribe to thread (unhandled) exception events
    ThreadException Handler handler = new
    ThreadException Handler();
    Application.Thr eadException += new
    ThreadException EventHandler(ha ndler.Applicati onThreadExcepti on);

    Application.Run (new FormMain());
    }

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

    public void ApplicationThre adException(obj ect sender,
    ThreadException EventArgs e)
    {
    try
    {
    unhandeledExcep tion = true;
    ShowThreadExcep tionDialog(e.Ex ception);
    }
    finally
    {
    // Fatal error, terminate program
    Application.Exi t();
    }
    }

    /// <summary>
    /// Creates and displays the error message.
    /// </summary>
    private void ShowThreadExcep tionDialog(Exce ption ex)
    {


    from with FormMain I do the following if there is an exception which
    is to not save the users settings -

    private void OnClosing(objec t sender, EventArgs ev)
    {
    if (Program.Thread ExceptionHandle r.UnhandeledExc eption)
    return;

    // save user settings...
    }

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

    public void FormMain_Load(o bject sender, EventArgs e)
    {
    thread = new System.Threadin g.Thread(MyThre ad);

    thread.Start();
    }

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

    Why not?

    Thanks as ever,

    Aine

  • Peter Duniho

    #2
    Re: Catching unhandled exception from within a thread

    On 2007-11-13 01:27:47 -0800, aine_canby@yaho o.com said:
    [...]
    The problem though is that if I create a thread as follows, exceptions
    are not being handled by ThreadException Handler -
    >
    [...]
    Why not?
    Just to be clear: the Application.Thr eadException 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.Set UnhandledExcept ionMode 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

    Comment

    Working...