代码之家  ›  专栏  ›  技术社区  ›  aybe

RuntimeWrappedException与UnhandledException

  •  1
  • aybe  · 技术社区  · 14 年前

    这是处理这两种情况的最佳方法吗?

     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)
                {
                    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;
        }
    }
    

    谢谢您!

    2 回复  |  直到 14 年前
        1
  •  1
  •   Hans Passant    14 年前

    main()方法中的异常处理程序将永远不会运行。Dispatcher.UnhandledException处理程序将阻止app.run()因异常退出。在事件处理程序中设置e.handled=true。不要让它沉默,让用户知道发生了什么事情。

    “unhandledsinstalled”变量是奇怪的btw。您安装了一个变量,它总是正确的。如果代码崩溃 之前 那么,没有理由尝试做一些有意义的事情 真正地 糟糕的事情发生了。

        2
  •  0
  •   aybe    14 年前

    我想我做对了,但是我还没能测试runtimewrappedException, 我刚刚发现DispatcherUnhandledException处理应用程序中的所有内容,我认为它严格适用于Dispatcher.BeginInvoke()的内容。

    谢谢您,

            [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;
        }