English 中文(简体)
作为控制机制的例外
原标题:Exceptions as a control mechanism
问题回答

我认为,出于以下几个原因,一般应保留例外情形:

  1. Exceptions have a high performance overhead (though that might not really a problem when e.g. dealing with files)
  2. Having an application throing and swallowing a high amount of exceptions can make debugging very hard
    • You might swallow the exception you are looking for somewhere
    • It can make it kind of hard for others to follow the programmflow, exspecially if the exceptions are catched higher in the call-hierarchie

当然,在您的具体案例中,自查询<代码>File.Exitsts()以来,可能有理由依赖例外情况,事先不保证在查阅档案时档案存在,因此,你可能必须列入例外情况。

我现在把整个“例外情况”搁置一边,简单地分析你的情况,以说服你正确无误,你只应在例外情况下使用例外。

在你看来,

while (!opened) { 
   try {
       <file_open>;
       opened = true
   } catch (exception) {
       //ignore
   }
}

你们最后吃掉了万国邮联的所有时间。 如果是,

while (!opened) { 
   if (file.exists) {
       <file_open>
       opened = true
   } else {
       Thread.sleep(<some_time>);
   }
}

你们会玩笑,把你未使用的时间留给其他进程,使万国邮联保持在最低限度。

因此,我认为,我这样说,首先进行测试是一个非常好的想法。

我将重新核对档案。 顺便说一句,例外的道路可能非常昂贵,否则就会非常昂贵。

例外情况属于例外情况。

你们需要的是测试;对此没有任何例外,如果是档案的话。 极端主义。





相关问题
handling exceptions IN Action Filters

Is there a better way to handle exceptions that occur inside an Action Filter itself in ASP .NET MVC? There re 2 ways I can think of at the moment. Using a try catch and setting the HTTP Status ...

既可捕获,又可举出例外。

我有一种办法,可以进入亚洲开发银行,因此,我国的亚行在多瑙河航道中的所有 st子都位于一个试捕区。 它正在追捕Kexception

Cross compiler exception handling - Can it be done safely?

I am doing some maintenance on a C++ windows dll library that is required to work with different VC++ compilers (as I don’t want to address different mangling schemes). I have already eliminated any ...

File Handling Issue

I am developing a tool in c#, at one instance I start writing into a xml file continuously using my tool,when i suddenly restart my machine the particular xml file gets corrupted, what is the reason ...

Watch a memory location/install data breakpoint from code?

We have a memory overwrite problem. At some point, during the course of our program, a memory location is being overwritten and causing our program to crash. the problem happens only in release mode. ...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签