This project is read-only.

Exceptions in VM are not being caught by Application_UnhandledException


We are seeing an issue where in some cases exceptions thrown by a VM are not caught in our UnhandledException handler in App.xaml.cs

An example of this would be adding a throw new Exception() to a method on button click, execution continues without entering the handler. Things running on the UI thread (we have some calls using Dispatcher) seem to bubble up to there.

Is there something in the framework that would be capturing these and is there a way we can expose them as we use the handler to log telemetry around the exception.

This is a Windows Phone 8 application, using 3.1.5


ipjohnson123 wrote May 30, 2014 at 11:13 PM

Hi Kyle,

There are a number of places in 3.0 where it catches exceptions and then logs them but doesn't re-throw it. For 4.0 I'm going to be changing the behavior to not catch and eat exceptions.

That doesn't really help you right now.

How are you connecting the button click to a method EventHandlers.Attach? If so I believe I remember that catches exceptions and logs them.

I'm hesitant to change the default behavior, but I think we can probably come up with something to let you specify you want to throw exceptions. Maybe a static property that you can set on startup to augment the behavior.

EventHandlers.ThrowsException = true;

For a while I was on the kick of trying to catch and log exceptions and in the end it caused more problems by hiding errors than it did if I had just let them bubble.

KJee85 wrote May 30, 2014 at 11:26 PM

Ok, thanks. Yeah I like the idea of having it be an opt-in property.

Where is the log, is it something we could parse at some point and upload to our DB?

ipjohnson123 wrote Jun 2, 2014 at 2:39 AM

Sorry thought I replied back to this the other day but apparently I didn't.

So the logging in 3.x is done through the DI container and you can implement the ILogAppender interface. When using a StyleMVVM project template it will create a debug log appender that will stop the debugger when an exception is logged.

For 4.0 I simplified and removed the dependency on the container (it made things way to complicated) and you can provide your own ILogService.