Windows 窗体应用程序中异常处理的最佳实践?

编程入门 行业动态 更新时间:2024-10-07 12:22:59
本文介绍了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:

  • 我应该什么时候重新抛出异常?
  • 我是否应该尝试拥有某种中央错误处理机制?
  • 与预先测试磁盘上的文件是否存在等内容相比,处理可能抛出的异常是否会影响性能?
  • 是否应该将所有可执行代码都包含在 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.

如果可行,抢先检查总是正确的,但并不总是完美的.例如,在检查文件存在的代码和打开文件的下一行之间,文件可能已被删除或其他一些问题可能会阻碍您的访问.在那个世界中,您仍然需要 try/catch/finally.根据需要同时使用抢先检查和 try/catch/finally.

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;

归根结底,运行中的应用程序抛出的绝大多数异常都应该暴露在某个地方.它们不应该暴露给最终用户(因为它们通常包含专有或其他有价值的数据),而是通常被记录下来,并通知管理员异常.可以向用户显示一个通用对话框,可能带有参考编号,以保持简单.

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 学到的一些技巧,这些技巧不止一次拯救了我的培根.您的里程可能会有所不同.

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 窗体应用程序中异常处理的最佳实践?

本文发布于:2023-11-05 10:32:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560595.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:窗体   应用程序   异常   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!