【问题标题】:Exception Handling and User Friendly messages异常处理和用户友好消息
【发布时间】:2009-07-23 01:27:24
【问题描述】:

我正在开发一个 winforms 应用程序,我想知道异常处理的最佳实践是什么。每当发生异常时,我都会打开一个异常对话框,显示必要的信息,即消息和堆栈跟踪。我的主要困惑是,我希望用户只看到一条友好的消息,但同时确保开发人员可以获得调试所需的数据。最好的方法是什么?

【问题讨论】:

    标签: winforms exception-handling


    【解决方案1】:

    我没有亲自使用过它,但Red Gate's Exception Hunter 看起来是一个很酷的工具。您最好的选择可能是将错误记录到磁盘,以便在有人需要查看它时可用,但不会以您的用户的方式弹出。如果需要,您可以打开一个窗口,要求用户通过您的网站向您提交异常信息和堆栈跟踪(只需单击“确定”)。避免发送私人信息,这可能意味着不发送参数值。

    编辑:哦,避免说“异常和堆栈跟踪”。说“发生了错误,但在您的帮助下,我们可以更快地修复它。您想此时自动将错误信息发送给____?请注意,错误报告不会传输任何个人信息。您可以单击“详细信息'以显示报告的完整信息。”如果他们单击详细信息,则垂直展开窗口以显示包含数据的只读文本框。

    【讨论】:

      【解决方案2】:
      catch (Exception ex)
      {
          cApp.DB.LogException(ex);
          Messagebox.Show(...);
      }
      

      cApp.DB.LogException(ex) 记录到数据库表中,除非数据库已关闭,否则会将其添加到文本文件中。

      【讨论】:

        【解决方案3】:

        我在用 WinForms 编写的实用程序中使用了一个实用方法。稍加注意,它可能在生产 WinForms 应用程序中很有用(让专家不要放过他们的批评):

        便利性重载:

        private void PerformUIAction(Action action)
        {
            PerformUIAction(action, (string) null);
        }
        
        private void PerformUIAction(Action action, string message)
        {
            PerformUIAction(action, () => message);
        }
        

        真实的:

        private void PerformUIAction(Action action, Func<string> messageHandler)
        {
            var saveCursor = Cursor;
            Cursor = Cursors.WaitCursor;
            try
            {
                action();
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    messageHandler() ?? ex.Message,
                    "Exception!",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error,
                    MessageBoxDefaultButton.Button1,
                    MessageBoxOptions.DefaultDesktopOnly);
                // Replace with logging code. The important part is ex.ToString(),
                // not ex.Message
                Debug.WriteLine(ex.ToString(), "Exception");
                throw;
            }
            finally
            {
                Cursor = saveCursor;
            }
        }
        

        使用示例:

        private void _samplesMenu_AfterSelect(object sender, TreeViewEventArgs e)
        {
            PerformUIAction(
                delegate
                    {
                        // Do the real work of the event in here.
                        // You can reference sender and e
                    },
                delegate
                    {
                        return string.Format(
                            "Error while processing action {1} for node {0}", 
                            e.Node.FullPath, e.Action);
                    });
        }
        

        【讨论】:

        • 这与简单的 try catch 有何不同?您正在将一种语言结构更改为另一种以产生相同的效果..
        • 它显示“等待”光标,执行操作,在 MessageBox 中显示任何异常。
        • 嗯,是的,我当时忽略了它,已经在我的解决方案中实现了这个:P
        【解决方案4】:

        感谢您的回答...我想将堆栈转储到某个日志文件中并显示仅对用户友好的消息在我的情况下会起作用:)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-04-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-24
          • 1970-01-01
          • 2012-10-08
          相关资源
          最近更新 更多