处理异常的方法 [英] Ways to handle exception

查看:120
本文介绍了处理异常的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了日志记录,什么构成处理异常?我问,因为人们说只能抓住一个你可以处理的例外。



例如,我写了一个与Active Directory进行交互的工具。我在域控制器上运行它。由于我对AD有深刻的了解,所以我可以处理一个其他异常情况(例如我可以提出请求另一个域名的提示),然后从那里开始。但是,如果生产服务器上的域名出现问题,那么这样很重要,那这不是特例吗?



所以在这种情况下环境问题应该是例外(鉴于是生产和AD等),但这是我可以处理的。我认为处理一个例外取决于程序受众(同意)?



无论如何,主要的问题是:推断如果我可以处理异常,我需要知道什么句柄需要 - 除了记录和呈现用户另一个选择(在这种情况下,我避免例外使用如果文件存在等)。



对于上述情况(AD ),我将我的代码结构化为:

  if(adIsAvailable)
// do whatever here

else
引发异常并要求采取行动

这是在gui



有关该设计的有效性的任何想法?

解决方案

你这里有一些不同的问题。


  1. 如何处理(并决定何时处理)基础代码抛出的异常

  2. 何时自动抛出异常

  3. 生产代码是否应该处理异常情况diff来自开发商。

关于点(1):



这可能有点混乱,很难决定 - 不同的API可能使用异常,即使是在同一种语言中。



在C#中,我喜欢使用 int.TryParse()而不是 int.Parse()并捕获一个 FormatException 如果我期望我的输入可能会解析失败,我想编写代码来处理这种情况。



如果我不想处理不好的输入,我将使用 int.Parse(),让异常传播。



这取决于



关于点(2):



基本上例外意思是我放弃。 (3):

>

我认为这几乎总是一个坏主意。



Aside:



我不同意活力回答的一部分,其中说:


最好的方法是检查前提条件,如果违反(文件
不存在)将抛出异常,而不是执行实际
任务,并依赖于通知的异常机制。


如果您编写如下代码:

  if(file_exists(x))
{/ * do something * /}
else
{/ * whatever * /}

然后你打开自己达到竞争条件。
也许这个文件在 file_exists()检查之间被删除,所以你的代码将会抛出异常。



或者也可以在您输入 else 部分之后创建文件。



这个,我认为做你想要做的更好,如果出了问题,就处理例外。


Other than logging, what constitutes handling an exception? I ask as people say only catch an exception you can handle.

For example, I wrote a tool to interact with Active Directory. I run it on the domain controller. As I have intimate knowledge of AD, an otherwise-exceptional case I can handle (eg I can raise a prompt to ask for another domain name) and go from there. But if there is a problem with the domain on a production server so critical, would this not be exceptional?

So in this case, a problem with the environment should be exceptional (given that is production and AD etc), yet this is something I can handle. I think handling an exception depends on the program audience (agree)?

Anyway, the main question: to deduce if I can "handle" an exception, I need to know what handle entails - other than logging and presenting the user with another choice (in which case I avoid exceptions by using if file exists etc).

For the above case (AD), I structured my code as:

if (adIsAvailable)
  // do whatever here

else
  raise exception and ask for action

this is then caught in the gui

Any thoughts on the validity of that design?

解决方案

You have a few different issues combined, here.

  1. How to handle (and decide when to handle) exceptions that are thrown by underlying code
  2. When to throw exceptions yourself
  3. Whether production code should treat 'exceptional' situations differently from dev. code.

Regarding point (1):

This can be a bit messy and hard to decide - different APIs might use exceptions differently, even inside the same language.

In C#, for instance, I prefer to use int.TryParse() rather than int.Parse() and catching a FormatException if I expect my input to maybe fail parsing, and I want to write code to handle that case.

If I don't want to handle bad input, I will use int.Parse(), and let the exception propagate.

It depends on the situation, alas.

Regarding point (2):

Exceptions basically mean "I give up". They mean something went wrong, but you aren't going to handle it yourself right there.

Regarding point (3):

I think this is almost always a bad idea.

Aside:

I disagree with part of Vitality's answer, where it says:

the best way is to check a precondition that if violated (file does not exist) will throw an exception, rather than performing the actual task and relying on the exception mechanism for notification.

If you write code like:

if (file_exists(x))
{ /* do something */ }
else
{ /* whatever */ }

Then you open yourself up to race conditions. Maybe the file was deleted in between the file_exists() check, so your code will throw an exception anyway.

Or maybe the file got created just after you entered the else section.

In a case like this, I think it is better to do what you are trying to do, and if something goes wrong, deal with the exception.

这篇关于处理异常的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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