在Windows中异常处理的最佳实践窗体应用程序? [英] Best Practice for Exception Handling in a Windows Forms Application?

查看:88
本文介绍了在Windows中异常处理的最佳实践窗体应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在写我的第一个Windows窗体应用程序的过程。我读过一些C#的书,所以我现在已经有了什么语言功能C#有比较好的了解了处理异常。他们都相当的理论但是所以我没有得到又是如何的基本概念转化为我的应用程序了良好的异常处理模型的感觉。

I'm currently in the process of writing my first Windows Forms application. I've read a few C# books now so I've got a relatively good understanding of what language features C# has to deal with exceptions. They're all quite theoretical however so what I haven't got yet is a feel for how to translate the basic concepts into a good exception-handling model in my application.

有没有人愿意分享关于这个问题的智慧珍珠的?张贴在处理异常你见过像我这样的新手做任何常见的错误,以及任何一般建议的方式,将我的应用程序更加稳定和强劲。

Would anyone like to share any pearls of wisdom on the subject? Post any common mistakes you've seen newbies like myself make, and any general advice on handling exceptions in a way that will my application more stable and robust.

目前,我试图找出的主要事情是:

The main things I'm currently trying to work out are:


  • 我什么时候应该重新抛出异常?

  • 我应该尝试有某种中央的错误处理机制?

  • 请处理异常可能被抛出了一个性能命中与pre-先发制人测试之类的东西在磁盘上的文件是否存在相比?

  • 如果所有可执行code被封闭在的try-catch-finally块?

  • 是否有任何时候,一个空的catch块是可以接受的?

所有建议感激地接受!

推荐答案

一个几个位...

您绝对应该有一个适当的集中异常处理政策。这可以作为一个try / catch包装的Main()一样简单,以优美的错误信息给用户不快。这是不得已而为之的异常处理程序。

You absolutely should have a centralized exception handling policy in place. This can be as simple as wrapping Main() in a try/catch, failing fast with a graceful error message to the user. This is the "last resort" exception handler.

preemptive检查是如果可行的话总是正确的,但并不总是完美的。例如,code,你检查文件的存在,并在那里你打开它的下一行之间,该文件可能已被删除或一些其他问题可能阻碍您的访问。你仍然需要的try / catch /终于在那个世界。同时使用preemptive检查和的try / catch /终于为宜。

Preemptive checks are always correct if feasible, but not always perfect. For example, between the code where you check for a file's existence and the next line where you open it, the file could have been deleted or some other issue may impede your access. You still need try/catch/finally in that world. Use both the preemptive check and the try/catch/finally as appropriate.

从不吞了异常,除了在最充分证明的情况下,当你绝对,积极确保所抛出的异常是宜居。这将几乎不会是这种情况。 (如果是,请确保你只吞下的具体的异常类 - 不要的曾经的燕子 System.Exception的

Never "swallow" an exception, except in the most well-documented cases when you are absolutely, positively sure that the exception being thrown is livable. This will almost never be the case. (And if it is, make sure you're swallowing only the specific exception class -- don't ever swallow System.Exception.)

在创建库(由您的应用程序使用),不要吞咽异常,不要害怕让异常冒泡。除非你有有益补充一点,不要再扔。永远不要(在C#)做到这一点:

When building libraries (used by your app), do not swallow exceptions, and do not be afraid to let the exceptions bubble up. Do not re-throw unless you have something useful to add. Do not ever (in C#) do this:

throw ex;

当你将删除调用堆栈。如果必须重新掷(这有时需要,如使用异常处理企业库座时),使用以下命令:

As you will erase the call stack. If you must re-throw (which is occasionally necessary, such as when using the Exception Handling Block of Enterprise Library), use the following:

throw;

在一天结束时,很大部分由一个正在运行的应用引发的异常应某处露出。他们不应该被暴露给最终用户(因为他们常常包含专有或其他有价值的数据),而是通常会记录,以通知的例外管理员。用户可以使用一个通用对话框psented $ P $,也许有参考号,让事情变得简单。

At the end of the day, the very vast majority of exceptions thrown by a running application should be exposed somewhere. They should not be exposed to end users (as they often contain proprietary or otherwise valuable data), but rather usually logged, with administrators notified of the exception. The user can be presented with a generic dialog box, maybe with a reference number, to keep things simple.

在.NET异常处理是艺术多于科学。每个人都会有自己的最爱在这里分享。这些只是几个小技巧我拿起使用.NET自第1天,已保存在多个场合我熏肉技术。你的情况可能会有所不同。

Exception handling in .NET is more art than science. Everyone will have their favorites to share here. These are just a few of the tips I've picked up using .NET since day 1, techniques which have saved my bacon on more than one occasion. Your mileage may vary.

这篇关于在Windows中异常处理的最佳实践窗体应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆