-1

Im trying to add some exceptionhandling in my win 8.1 universal app. From what I undersatnd from reading here at the forum I should be able to add for examlple something like this in my App.xaml.cs:

AppDomain.CurrentDomain.UnhandledException += (s, e) =>
    LogUnhandledException((Exception) e.ExceptionObject, "AppDomain.CurrentDomain.UnhandledException");

or: (from msdn)

// Add the event handler for handling UI thread exceptions to the event.
    Application.ThreadException += new ThreadExceptionEventHandler(ErrorHandlerForm.Form1_UIThreadException);

    // Set the unhandled exception mode to force all Windows Forms errors to go through 
    // our handler.
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

The problem is that i cannot acess either AppDaomain or Application.ThreadException in my App.xaml.cs-file.

Does anyone knows what i am missing here? Thanks a lot!

EDIT:

From what I read in this thread :

http://stackoverflow.com/questions/13636751/is-there-a-global-exception-handler-in-windows-store-apps

This seems to be a way to do something similar:

 public App() 
        {
            this.InitializeComponent();
            this.Suspending += this.OnSuspending;
            this.UnhandledException += App_UnhandledException;   
        }

        void App_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            //This method never seems to get hit
        }

However,it does not seem to be enough, the methid never gets hit. Does this mean that there are no exceptions occuring?

Wranglerino
  • 199
  • 1
  • 11
  • http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.application.unhandledexception – Hans Passant Nov 25 '14 at 16:51
  • Thank you, the link explains that I should use: public event UnhandledExceptionEventHandler UnhandledException But there is no code or explanation of how tom implement it i the project. – Wranglerino Nov 25 '14 at 17:05
  • If you have no idea what to do then the best thing to do is *nothing*. The app terminates and you can get crash statistics from your Windows Store developer portal. – Hans Passant Nov 25 '14 at 17:57
  • You make a good point of course. Still I would like to have a way to log all uncaught exceptions to a file. Maybe someone can demonstrate how to set up the event the correct way? – Wranglerino Nov 25 '14 at 18:04
  • Please see EDIT in question – Wranglerino Nov 25 '14 at 19:54

1 Answers1

1

Remember that unhandled exceptions ONLY fire if they aren't inside a try/catch. If you want to report when other exceptions are occurring, inside your catch(Exception ex), add a call to send your exception.

Daniel Gary
  • 507
  • 4
  • 10
  • Thank you, does this mean that IF an unhandled exception occurs in my code that is NOT inseide a try/catch, that my method would have been hit? – Wranglerino Nov 25 '14 at 20:04
  • Yes, that is correct. The App_UnhandledException method will only fire if an exception is NOT handled by your code. A try/catch will suppress an exception from crashing your app. – Daniel Gary Nov 25 '14 at 20:06
  • Ok! So in order for me to see it work in action is to create something in my app that is guranteed to make an exception. Have you got anything in mind that could do the trick? – Wranglerino Nov 25 '14 at 20:09
  • Sure. Just put a throw(new Exception("Test exception")) anywhere in your code and see what happens :) – Daniel Gary Nov 25 '14 at 22:29