是什么[仅限抓住你可以处理异常"究竟意味着什么? [英] What does "Only catch exceptions you can handle" really mean?

查看:107
本文介绍了是什么[仅限抓住你可以处理异常"究竟意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我任务是写一个异常处理策略和指南文档,我的工作的.NET / C#项目。我有它一个艰难的去。有大量的可用信息如何/何时抛出,捕获,包装异常,但我要找的描述应该去catch块短包裹里面什么样的东西,抛出异常。

I'm tasked with writing an Exception Handling Strategy and Guidelines document for a .NET/C# project I'm working on. I'm having a tough go at it. There's plenty of information available for how/when to throw, catch, wrap exceptions, but I'm looking for describing what sorts of things should go on inside the catch block short of wrapping and throwing the exception.

try
{
   DoSomethingNotNice();
}
catch (ExceptionICanHandle ex)
{
   //Looking for examples of what people are doing in catch blocks
   //other than throw or wrapping the exception, and throwing.
}



在此先感谢

Thanks in advance

推荐答案

这意味着这一点。如果你期望的代码你正在运行抛出一个异常,而当异常被抛出你的代码知道什么地方出了错,以及如何着手,然后捕获异常并处理它。

It means exactly that. If you are expecting code you're running to throw an exception, and when that exception is thrown your code knows what went wrong and how to proceed, then catch the exception and handle it.

基本上,该规则的存在是为了防止反模式,如:

Basically, the rule exists to prevent anti-patterns like:

try
{
   ...
}
catch(Exception ex)
{
   throw;
}



渔获这里什么都不做,但减速带添加到展开调用堆栈。如果你实际上并不想与你捕捉异常任何东西,你不应该甚至抓费心了。

The catch here does nothing but add a speed bump to unwinding the call stack. If you don't actually want to anything with the exception you're catching, you shouldn't even bother with the catch.

一个相关的,但更有效的情况下在那里你不关心异常被抛出,但你需要在所有的情况下,进行清理。在这种情况下,跳过捕;你不需要它,只是让一个try-finally块

A related but far more valid case is where you don't care about the exception being thrown, but you need to clean up in all cases. In that case, skip the catch; you don't need it, just make it a try-finally block.

编辑:要回答在岗的问题,而不仅仅是主题,你可以写一个规则如下:请不要在编写一个try-catch语句没有做任何事情,否则只能重新抛出捕获的异常所有catch语句应该执行有关抛出的异常一些增值行动

To answer the question in the post, not just the subject, you could write a rule as follows: "Do not code a try-catch statement that does not do anything, or only rethrows the caught exception. All catch statements should perform some value-added action relating to the thrown exception."

例如,假设您正在试图连接到使用由用户提供的,当他们登录到你的应用程序凭据的SQL Server实例。 。事情可以去错了几十个,其中有一些你不能指望,你应该

For example, let's say you are trying to connect to a SQL Server instance using credentials supplied by the user when they log into your app. Dozens of things could go wrong, some of which you can't expect, some of which you should.


    部分
  • 服务器没有响应 - 你可以再次尝试; 。也许递归调用的连接方法在catch,着有重试计数器打破,否则无限循环

  • 用户身份验证失败 - 显示友好的(或​​不那么友好,但红色简洁易懂)消息对话框中的

  • 用户未授权连接到指定的DB - 取决于你的安全设置。在大多数办公室,这是你应该发送电子邮件有关的DBA,因为这意味着他创建的登录,但忘记分配适当的权限

  • 网络不可用:你可以提醒用户通过登录对话框或一个新的对话框错误,零重试几次,等

  • 司 - 跆拳道?还有什么比一个登录时可能导致被零的DIV?你没想到这个例外,你不知道什么地方出了错在这种情况下,因此无法继续运行的代码,所以不要抓住它。

  • 如果有什么差错,你可能希望将消息记录到文件或审计/安全目的共享的资源。这项工作应在较低水平,如果你想继续执行,或更高的水平,如果你要正常关闭之后发生的。

  • Server isn't responding - you can try again; perhaps call the connection method recursively in the catch, with a "retry counter" to break the otherwise infinite loop.
  • User failed authentication - show a friendly (or not-so-friendly, but concise and understandable) message in red on the dialog box.
  • User not authorized to connect to the specified DB - Depends on your security setup; in most offices, that's something you should e-mail the DBA about, because it means he created the login but forgot to assign the proper rights.
  • Network not available: You can alert the user through an error on the login dialog or a new dialog, retry a couple times, etc.
  • Division by zero - WTF? What could possibly cause a Div by Zero during a login? You're not expecting this exception, you have no clue what went wrong in this case and therefore can't continue running code, so don't catch it.
  • If anything goes wrong, you may want to log the message to a file or a shared resource for audit/security purposes. This should happen at lower levels if you want to continue execution, or higher levels if you're going to gracefully shut down afterward.

所有这些例子涉及第一卡已知类型的异常并询问它,看看究竟出在哪里,然后进行一些已知的操作,可允许程序继续执行。目的是为了防止应用程序崩溃和燃烧出问题的时候,你知道可能出问题,但知道如何保持在这种情况下运行的程序。

All of these examples involve first catching an exception of a known type and interrogating it to see what exactly went wrong, then performing some known action that can allow the program to continue execution. The object is to prevent the application from crashing and burning when something goes wrong that you know could go wrong, but know how to keep the program running in that case.


  1. 如果您不希望一个例外,不抓有一个
  2. $ b $:用于捕获异常的基本规则b
  3. 如果您不能或不想继续执行代码接收到异常后,无论你是否知道它可以发生与否,不抓​​住它。

  4. 如果你期待发生异常,并知道如何继续执行的代码,当它发生(至少在一段时间),然后捕捉并为了做到这一点执行你需要的任何特殊的操作。

  5. 从不异常陷阱(空catch块);导致应用程序以更不可预知的方式默默的失败。

  6. 永远离开接球再次抛出在生产代码(只有一个重新抛出的catch块)。在调试的时候,因为他们让你找出那些失败的代码特定部分,他们有时是有用的,但是在生产代码,他们只是减速带,以抛出或实际处理的除外。

  1. If you aren't expecting an exception, don't catch one.
  2. If you can't or don't want to continue execution of code after receiving an exception, whether you know it can happen or not, don't catch it.
  3. If you are expecting the exception to occur, and know how to continue executing code when it happens (at least for a while), then catch and perform any special actions you need in order to do so.
  4. NEVER trap exceptions (an empty catch block); that causes applications to fail silently in even more unpredictable ways.
  5. NEVER leave catch-and-rethrow (a catch block with only a rethrow) in production code. They can sometimes be useful when debugging as they allow you to identify specific segments of code that are failing, but in production code they're just a speed bump to throwing out or actually dealing with the exception.

这篇关于是什么[仅限抓住你可以处理异常"究竟意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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