0

Is there a way to catch Exceptions from Objects declared in XAML such as a StoryBoard and keep the application from crashing completely?

In this particular case users will rarely encounter an exception relating to an un-animatable or unaccessible property path. I am working to address these issues but in general the app is critical and I would like to prevent it from simply crashing in these instances.

The app is a UserControl that is Hosted in a windows forms app via ElementHost.

How do you handle these types of exceptions and keep the app running?

Some additional info I tried using something like:

Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;    

as a means of catching the exceptions but Application.Current is always null so I can't use it.

In a nutshell I need to ensure that no matter what happens the app itself continues to run.

jrandomuser
  • 1,510
  • 19
  • 50
  • 1
    Hi could you please post your code? – Stígandr Aug 20 '14 at 16:12
  • [This might help](http://stackoverflow.com/questions/6291933/catch-application-exceptions-in-a-windows-forms-application). – Arian Motamedi Aug 20 '14 at 16:14
  • 1
    I don't have any code because I don't know what is throwing the exception. The question is what code do you use to Catch Exceptions that are occurring in your view? Add a StoryBoard to a UserControl and animate the color of shape all from XAML. If that were to blow up and you wanted to handle it, how would you? – jrandomuser Aug 20 '14 at 16:16
  • @PoweredByOrange His example is for WinForms but I did see a reference to AppDomain.UnhandledException Event that I will look at. – jrandomuser Aug 20 '14 at 16:23
  • @jrandomuser See the MSDN example linked below, it shows how to handle AppDomain.Current.UnhandledException in a WinForms application. – Stígandr Aug 20 '14 at 16:33

1 Answers1

3

Oh the horror when you get xaml related crash errors! :) I don't have the full receipt in my head here right now, and I need more information about your issue, but make sure to hook on to all following error handlers(App.xaml.cs in WPF, check link below for forms).

DispatcherUnhandledException += UnhandledDispatcherException;
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

private void UnhandledDispatcherException(Object sender, DispatcherUnhandledExceptionEventArgs e)
    {
        // do your stuff here!
        e.Handled = true; // Ex is now handled and will not crash your app
    }

This one is forms only I think(dont have my devbox here).

Application.ThreadException += UnhandledThreadException

Add your handlers and log/System.Diagnostics.Trace.WriteLine/breakpoint away!

Check this example from MSDN regarding AppDomain. Verify that AppDomain.Current is not null when starting as well.

Snippet:

public class Example 
{
   [SecurityPermission(SecurityAction.Demand,Flags=SecurityPermissionFlag.ControlAppDomain)]
   public static void Main()
   {
     AppDomain currentDomain = AppDomain.CurrentDomain;
     currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
   ...
   }
}

You may also do this after your InitializeComponents, if Application.Current is null.

if (System.Windows.Application.Current == null)
    new System.Windows.Application();

And ofc check your debug output! :)

Hope it helps,

Cheers

Stian

Stígandr
  • 2,874
  • 21
  • 36
  • Thanks! Part of my issue has been that this app runs in a very controlled environment. Physical access is restricted, writing to file and email are not permitted. When an exception occurs we get a call. The only details we have are the event log which must be copied by hand if we want it. The Control runs on a WinForm as an MDI child. In the event of "any exception" the app is supposed to at least handle it gracefully by not crashing. Though ideally I'd like to log to event log still. XAML presented me the challenge of elements that can't necessarily be wrapped in a simple try{}. – jrandomuser Aug 20 '14 at 17:06
  • @jrandomuser There are some issues integrating them, particulary the other way around because you loose stuff like ZIndex etc. But you have all your handlers up there, just set them to handle the exceptions, if you want your app to move on. I updated the answer with an example. – Stígandr Aug 20 '14 at 17:09