I'm trying to log all exceptions in a xml file, so what I did is create a UnhandledException
method that will be calling every time an exception is generated, a little example:
public MainWindow()
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionTrapper);
InitializeComponent();
throw new Exception("this is an example");
}
public static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e)
{
Console.WriteLine(e.ExceptionObject.ToString()); //Here there is breakpoint
Environment.Exit(1);
}
Now the problem is that the method UnhandledExceptionTrapper
is called only when an exception happen inside the main thread, infact I execute all the main methods inside different Tasks, an example:
Task.Factory.StartNew(() =>
{
//heavy operations
foo();
})
.ContinueWith((prevTask) =>
{
//final operations
});
now when an exception happen in foo()
for example:
public void Foo()
{
throw new Exception("another example");
}
the method UnhandledExceptionTrapper
it's not called.
On the official documentation:
Occurs when an exception is not caught.
I don't understand if this working on all application (also task), or just main thread.
Thanks for any explaination.