开发者

RuntimeWrappedException VS UnhandledException

Is this the best approach to handle both cases ?

 public static class Program
{
    //http://blogs.microsoft.co.il/blogs/arik/archive/2010/05/28/wpf-single-instance-application.aspx
    //http://social.msdn.microsoft.com/forums/en-US/wpf/thread/dee46bde-9baa-46b6-889c-04e20dd04029
    //http://msdn.microsoft.com/en-us/library/ms404228.aspx?appId=Dev10IDEF1&l=EN-US&k=k%28CS1058%29;k%28TargetFrameworkMoniker-%22.NETFRAMEWORK&k=VERSION=V4.0%22%29&rd=true

    [STAThread]
    public static Int32 Main(string[] args)
    {
        bool unhandledInstalled = false;
        try
        {
            AppDomain.CurrentDomain.UnhandledException += UnhandledException;
            unhandledInstalled = true;

            var app = new App();
            app.DispatcherUnhandledException += DispatcherUnhandledException;
            return app.Run();
        }
        catch (SecurityException)
        {
            // Notify
        }
        catch (Exception e)
        {
            var rwe = e as RuntimeWrappedException;
            if (rwe != null)
            {
                开发者_StackOverflow中文版object wrappedException = rwe.WrappedException;
                MessageBox.Show(wrappedException.ToString());
            }

            if (unhandledInstalled)
                throw;

            // No handler has been installed but handle it anyway.
            UnhandledException(null, new UnhandledExceptionEventArgs(e, true));
        }

        return -1;
    }

    private static void UnhandledException(object sender, UnhandledExceptionEventArgs args)
    {
        MessageBox.Show(args.ExceptionObject.ToString());
        Environment.Exit(-1);
    }

    private static void DispatcherUnhandledException(object sender,
                                                     DispatcherUnhandledExceptionEventArgs e)
    {
        // Do something here
        e.Handled = true;
    }
}

Thank you !


Your exception handler in the Main() method will never run. The Dispatcher.UnhandledException handler will prevent app.Run() from exiting due to an exception. You set e.Handled = true in the event handler. Don't make it silent, let the user know something went kaboom.

The "unhandledInstalled" variable is strange btw. You installed one, it is always true. If the code crashes before the try block then there's no reason to try to do something meaningful, something really bad happened.


I think I've got it right, I haven't been able to test RuntimeWrappedException though, and I just discovered that DispatcherUnhandledException handles everything inside the application, I thought it was strictly for Dispatcher.BeginInvoke() stuff.

Thank you,

        [STAThread]
    public static Int32 Main(string[] args)
    {
        try
        {
            AppDomain.CurrentDomain.UnhandledException += UnhandledException;

            var app = new App();
            app.DispatcherUnhandledException += DispatcherUnhandledException;
            int run = app.Run();

            return run;
        }
        catch (SecurityException)
        {
            // Notify & exit
        }
        catch (Exception e)
        {
            var rwe = e as RuntimeWrappedException;
            if (rwe != null)
            {
                object wrappedException = rwe.WrappedException;
                MessageBox.Show(wrappedException.ToString());
                Environment.Exit(-1);
            }

            throw;
        }

        return -1;
    }

    private static void UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        MessageBox.Show(e.ExceptionObject.ToString());
        Environment.Exit(-1);
    }

    private static void DispatcherUnhandledException(object sender,
                                                     DispatcherUnhandledExceptionEventArgs e)
    {
        MessageBox.Show(e.Exception.ToString());

        // Decide if we exit or not
        // ...

        e.Handled = true;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜