0

Anyone knows how to prevent app to be terminated when unhandled exception occurred from another thread? Seems doing AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException is only to capture when the error occurred but it won't handle it -- I mean it won't clear the error so the error keep continues.

Here's my workaround :

using System;
using System.Security.Permissions;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
    class ErrorForm : Form {
        private Button button2;
        Thread newThread = null;

        private ErrorForm() {
            InitializeComponent();
        }

        [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
        public static void Main(string[] args) {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Application.Run(new ErrorForm());
        }

        // Start a new thread, separate from Windows Forms, that will throw an exception. 
        private void button2_Click(object sender, EventArgs e) {
            newThread = new Thread(() => {
                throw new Exception("Aha!");
            });
            newThread.Start();
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
            MessageBox.Show("CurrentDomain_UnhandledException");
        }

        private void InitializeComponent() {
            this.button2 = new Button();
            this.SuspendLayout();
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(12, 12);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(260, 23);
            this.button2.TabIndex = 1;
            this.button2.Text = "Make Error";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += this.button2_Click;
            // 
            // ErrorHandlerForm
            // 
            this.ClientSize = new System.Drawing.Size(284, 52);
            this.Controls.Add(this.button2);
            this.Name = "ErrorHandlerForm";
            this.ResumeLayout(false);
        }
    }
}

When I run this code and press button2, then CurrentDomain_UnhandledException invoked as expected, but next it goes back again to throw new Exception("Aha!");. The best thing I can do is to kill the process in method CurrentDomain_UnhandledException like this:

    private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
        MessageBox.Show("CurrentDomain_UnhandledException");
        if (e.IsTerminating) {
            Process.GetCurrentProcess().Kill();
        }
    }

But it's not acceptable in my scenario of app. I want to the app (main form) kept live and this error smoothly handled. And I don't want to replace my logic done by Thread with BackgroundWorker. Is that possible? Any good solution for this?

Thank you in advance!

Adiono
  • 1,034
  • 11
  • 19
  • What do you mean it keeps coming back? Are you talking about the debugger? I know in older versions of Visual Studio, the debugger would sometimes get stuck and keep popping up the exception assistant repeatedly for the same exception. – siride Apr 30 '14 at 02:15
  • Yes it's on the debugger -- but when I execute the app out of debugger (CTRL+F5) after "CurrentDomain_UnhandledException" messagebox shown the app is crash. – Adiono Apr 30 '14 at 02:17
  • This [answer][1] is solved my problem. [1]: http://stackoverflow.com/a/15348805/1586914 – Adiono Apr 30 '14 at 02:21
  • Hmmm.. the answer above works but not for debugger.. – Adiono Apr 30 '14 at 02:30
  • 1
    How about catching and handling the exception in the thread where it's thrown, rather than waiting for it to escape the thread and put the rest of your program in jeopardy? – Jim Mischel Apr 30 '14 at 04:07
  • No Jim, cause the idea is about how to handle real 'unhandled exception'. I think I end up with solution as described on http://stackoverflow.com/a/15348805/1586914. – Adiono Apr 30 '14 at 06:19

0 Answers0