C# WPF Application Unhandled Exception

Hi,

I have a WPF application. I have a DispatcherUnhandledException to deal with any unhandled exceptions on the UI thread.

I however need to catch any unhandled exceptions on other threads. Looking on Google I can see that I should be using AppDomain.CurrrentDomain.UnhandledException. However I am being stupid and cannot get it to work.

My question is where in my application should this event be placed & how?

For example for the DispatcherUnhandledException it was quite simple, see code below. Think I'm missing something for the AppDomain.CurrrentDomain.UnhandledException. I have no come across AppDomain before.

<Application x:Class="UnhandledExceptionHandler.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml"
    Startup="Application_Startup"
    DispatcherUnhandledException="Application_DispatcherUnhandledException">
    <Application.Resources>
         
    </Application.Resources>
</Application>

Open in new window


Any help would be great!
Thanks,
M
mcs26Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roopesh ReddyIT AnalystCommented:
Hi,

DispatcherUnhandledException should be fine and it handles all the exceptions in the UI thread!

http://msdn.microsoft.com/en-us/library/system.windows.application.dispatcherunhandledexception.aspx

Hope it helps u...
0
mcs26Author Commented:
Hi,

But I still get unhandled errors in my application? For example I have thrown a divide by zero error which tells me is not handled by users code?

Thanks,
M
0
Gautham JanardhanCommented:
dispatcher only catches error in ui thread, but not in the ones u spawn. try AppDomain.CurrrentDomain.UnhandledException+= .... in app.xaml.cs constructor
0
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

mcs26Author Commented:
Hi Gauthampj,

Thanks for the reply. Stupid question but where is the constructor in app.xaml.cs?

Cheers
M
0
Gautham JanardhanCommented:
in your startup project, there will be App.xaml, app.xaml.cs will be the code behind for that
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mcs26Author Commented:
Ok thanks I've done that. I'm still getting an unhandled error when dividing by zero - this is on the UI thread throw.

Is there anything obviously wrong with both bits of my code below?

Thanks again!

<Application x:Class="QOE.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml"
             Startup="Application_Startup"
             Exit="Application_Exit"             
             DispatcherUnhandledException="Application_DispatcherUnhandledException"> 

Open in new window


public partial class App : Application
    {

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            AppDomain currentDomain = AppDomain.CurrentDomain;
            currentDomain.UnhandledException += new UnhandledExceptionEventHandler(currentDomain_UnhandledException);
        }

        void currentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            HandleException(e.ExceptionObject as Exception);
        }

        private void Application_Exit(object sender, ExitEventArgs e)
        {
            // application was closed safely
            SettingsSave(true);
        }

        private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            Exception ex = (Exception)e.Exception;
            HandleException(ex as Exception);
            //HandleException(e.ExceptionObject as Exception);

            SettingsSave(false);

            // error message for the user
            //MessageBox.Show("Exception: " + e.Exception.Message + ". Application will now close.", "Application Closing", MessageBoxButton.OK, MessageBoxImage.Stop);
            Application.Current.Shutdown();
        }

        private static void HandleException(Exception ex)
        {
            if (ex == null)
                return;
            
            ExceptionPolicy.HandleException(ex, "Default Policy");
            //MessageBox.Show(NavMonitor.Properties.Resources.UnhandledException, "NAV Monitor", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
            Environment.Exit(1);
        }

        private void SettingsSave(bool SafeClose)
        {
            // close the application & save settings of close
            QOE.Properties.Settings.Default.ClosedCorrectly = SafeClose;
            QOE.Properties.Settings.Default.Save();
        }

Open in new window

0
Gautham JanardhanCommented:
you are not setting e.Handled = true; in Application_DispatcherUnhandledException
0
mcs26Author Commented:
I've added that line in Appliction_DispatcherUnhandlerException but the code is still debugging with unhandled error by user code message.
0
Gautham JanardhanCommented:
in debug mode it will still break depending on ur setting in VS. but when u run the exe, the message that an unhandled exception occurred in application wont come up
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.