Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

How to handle C# AppDomain.UnhandledException

$
0
0

I have read these two pages from msdn: 12

So I constructed this simple Web Forms app:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        new Thread(new ThreadStart(throwException)).Start();           
    }

    public static void throwException()
    {
        throw new Exception();
    }

    public static void UnhandledExHandler(object sender, UnhandledExceptionEventArgs t) 
    {
        MessageBox.Show("This is exception is unhandled.");            
    }
}

static class Program
{        
    static void Main()
    {            
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Form1.UnhandledExHandler);
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

The problem with this is that after executing UnhandledExHandlerForms1() constructor does not return but keeps throwing Exception, I mean it keeps calling throwException() on a new thread. The end user experience popping of MessageBox continuously. I know I must call Application.Exit()inside UnhandledExHandler to exit the app. But shouldnt this stop calling UnhandledExHandlerafter called once?



Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>