That may happen when the exception is thrown on a secondary thread. See remarks section on this page:
Standalone or browser-hosted WPF applications use the Application class to detect unhandled exceptions (see DispatcherUnhandledException). However, Application can only detect unhandled exceptions that are thrown on the same thread that the Application class is running. Usually, an application will have one main user interface (UI) thread, so the unhandled exception detection behavior of the Application class is adequate. However, unhandled exceptions that are thrown on secondary threads are not automatically detected by the Application class on the main UI thread.
You can try using this event to catch detect the exception and log the error:
AppDomain.UnhandledException Event
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
static void MyHandler(object sender, UnhandledExceptionEventArgs args) {
Exception e = (Exception) args.ExceptionObject;
Console.WriteLine("MyHandler caught : " + e.Message);
}
UPDATE:
Apart from the threading issue it also can the cause if you put your try...catch block to the wrong place. Consider this example:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
Do();
}
private void Do()
{
try
{
int result = new ClassLibrary1.Class1().Calculate(2, 4);
}
catch (System.Exception ex)
{
Console.WriteLine("MyHandler caught by try...catch: " + ex.Message);
}
}
}
This will result in an exception at the line where the Do() is called since the CLR here tries to resolve the assembly at this point. The exception is not caught and the app terminates.
But if you try this one:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
try
{
Do();
}
catch (System.Exception ex)
{
Console.WriteLine("MyHandler caught by try...catch: " + ex.Message);
}
}
private void Do()
{
int result = new ClassLibrary1.Class1().Calculate(2, 4);
}
}
The output is:
MyHandler caught by try...catch: Could not load file or assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
Note that the UnhandledException event is not fired when you subscribe to it in the same function where the assembly is referenced. This works as well:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(
(sender, args) =>
{
Exception ex = (Exception)args.ExceptionObject;
Console.WriteLine("MyHandler caught by UnhandledException handler: " + ex.Message);
});
Do();
}
private void Do()
{
int result = new ClassLibrary1.Class1().Calculate(2, 4);
}
Result:
MyHandler caught by UnhandledException handler: Could not load file or assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
Hope it helps.