Voici un code assez robuste que nous conseillons aux utilisateurs d'utiliser lors de l'implémentation de http://exceptioneer.com dans leurs applications Windows.
namespace YourNamespace
{
static class Program
{
[STAThread]
static void Main()
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
HandleException(e.Exception);
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
HandleException((Exception)e.ExceptionObject);
}
static void HandleException(Exception e)
{
//Handle your Exception here
}
}
}
Merci,
Phil.
La réponse la plus pertinente pour Windows Services peut être trouvée ici: http://stackoverflow.com/a/5117790/986720 Il explique pourquoi l'exception n'est pas interceptée par AppDomainUnhandledException handler –