In WinForms I use:
- System.Windows.Forms.Application.ThreadException
- System.Windows.Application.UnhandledException
What should I use for non-Winforms multi-threaded application?
Consider the complete code below in C# .NET 4.0:
using System;
using System.Threading.Tasks;
namespace ExceptionFun
{
class Program
{
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Task.Factory.StartNew(() =>
{
throw new Exception("Oops, someone forgot to add a try/catch block");
});
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
//never executed
Console.WriteLine("Logging fatal error");
}
}
}
I have seen tons of similar questions on stackoverflow, but none contained a satisfactory answer. Most answers are of type: "You should include proper exeption handling in your code" or "Use AppDomain.CurrentDomain.UnhandledException".
Edit: It seems my question was misunderstood, so I have reformulated it and provided a smaller code example.