简体   繁体   English

WinForms / WPF中的异常处理

[英]Exception Handling in WinForms/WPF

What is the best practice for exception handling in WinForms or WPF. WinForms或WPF中异常处理的最佳实践是什么。 I haven't found a satisfying solution so far. 到目前为止,我还没有找到令人满意的解决方案。 Some people suggest to catch the unhandled exception event. 有人建议捕获未处理的异常事件。 But I don't think that this is a good solution. 但是我认为这不是一个好的解决方案。

The problem 问题

I have a 3 layer application. 我有一个3层应用程序。 The persistence layer, the business layer and the ui layer. 持久层,业务层和ui层。 Instead of catching exceptions in all three layers I think the correct place to catch the exceptions are in the ui layer. 我认为不是在所有三个层中都捕获异常,而是在ui层中捕获异常的正确位置。 So the user will get an error message and maybe there is also a way the user can 'solve' the issue (wrong data input, etc). 因此,用户将收到一条错误消息,也许用户还有一种方法可以“解决”问题(错误的数据输入等)。

So the question is, is the best/correct way to surround EVERY ui-eventhandler (click event, selection changed event, etc) with a try catch block? 所以问题是,用try catch块包围每个ui事件处理程序(单击事件,选择更改的事件等)的最佳/正确方法是吗? Like this: 像这样:

private void button_MouseUp(object sender, MouseButtonEventArgs e)
{
   try
   {
      /// ...
   }
   catch (Exception ex)
   {
      // log exception
      // show error to user if necessary
   }
}

I know that catching all exceptions is bad. 我知道捕获所有异常是不好的。 And of course you need to prevent exceptions by validating the user input data. 当然,您需要通过验证用户输入数据来防止异常。 But sometimes there will be bugs ;). 但有时会出现bug;)。

Imagine the following situation: The user makes some changes which aren't saved yet. 想象一下以下情况:用户进行了一些尚未保存的更改。 Then he clicks a button and in the button handler is a bug. 然后,他单击一个按钮,并且在按钮处理程序中是一个错误。 So wouldn't it be better to show a error message and let the user decide when he wants to quit the application, so that the user won't lose the unsaved data? 因此,显示错误消息并让用户决定何时要退出应用程序,这样用户不会丢失未保存的数据会更好吗?

So the question is, is the best/correct way to surround EVERY ui-eventhandler (click event, selection changed event, etc) with a try catch block? 所以问题是,用try catch块包围每个ui事件处理程序(单击事件,选择更改的事件等)的最佳/正确方法是吗?

No. The only thing you do is to repeat yourself (violation of DRY). 不。您要做的就是重复自己(违反DRY)。 If you in the future want to change how exceptions are logged you have to go through every method that has a try/catch block. 如果将来您想更改记录异常的方式,则必须遍历所有具有try / catch块的方法。

You can use the Application.ThreadException event to catch all unhandled exceptions (in the UI thread) to display an error dialog to the user and log the exception. 您可以使用Application.ThreadException事件捕获所有未处理的异常(在UI线程中),以向用户显示错误对话框并记录该异常。

The best way is of course to make sure that the user have supplied correct information by validating it. 最好的方法当然是通过验证来确保用户提供了正确的信息。 By doing so all exceptions that occurs are truly exceptions and not something that you could have prevented. 这样,所有发生的异常都是真正的异常,而不是您可以避免的异常。

Here are my golden rules: 这是我的黄金法则:

  1. Always try to prevent exceptions by making sure that the supplied data is correct. 始终通过确保所提供的数据正确来防止异常。
  2. Only catch exceptions that you can use to resolve the situation without user involvement. 仅捕获可用于解决情况的异常,而无需用户参与。
  3. Use Application.ThreadException event for all other errors (and log them). Application.ThreadException事件用于所有其他错误(并记录它们)。

You can read my exceptions series if you want to: http://blog.gauffin.org/2013/04/what-is-exceptions/ 如果您愿意,可以阅读我的例外系列: http : //blog.gauffin.org/2013/04/what-is-exceptions/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM