例外作为控制机制 [英] Exceptions as a control mechanism

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

问题描述

我正在阅读这篇文章,并笑了起来 http:// mcfunley。 com / 239 / exceptions-are-not-a-control-mechanism

I was reading this post and laughed http://mcfunley.com/239/exceptions-are-not-a-control-mechanism

在我的一个应用程序中,即使我期望也不使用File.Exist文件存在很多时间。我尝试创建一个文件而不覆盖旧的,如果它失败,我将其重命名为文件名(Try Number).ext和循环直到它打开。

In one of my apps i do not use File.Exist even tho i EXPECT files to exist a good deal of the time. I try to create a file without overwriting the older and if it fails i rename as Filename (Try Number).ext and loop until it opens.

我应该使用File。在这种情况下存在吗?或者我应该继续尝试打开一个文件,循环直到我这样写模式?

Should i used File.Exist in this case ? or should i continue to try opening a file, loop until i do then write pattern?

推荐答案

在我看来,例外应该是一般由于以下几个原因保留了真正特殊的情况:

In my opinion exceptions should generally be reserved for truely exceptional circumstances for several reasons:


  1. 异常具有高性能开销(尽管这可能不是真正的问题,例如处理文件)

  2. 让应用程序阻止并吞下大量异常可以使调试非常困难


    • 您可能会吞下例外,你正在寻找某个地方

    • 它可以使他人很难遵循程序流程,特别是如果异常在调用层次结构中被捕获更高

当然在您的具体情况下,依赖于例外情况可能是有道理的,因为检查 File.Exitsts()事先不保证访问该文件时存在,因此您可能包括例外情况

Of course in your specific case it might make sense to rely on the exceptions since checking File.Exitsts() beforehand doesn't guarantee that the file exists when it is accessed, so you might have to include the exceptional case anyhow

这篇关于例外作为控制机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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