简体   繁体   English

C#Cast Exception

[英]C# Cast Exception

Stumbled on some old code that is throwing and empty catching some cast exceptions (about 20 per trip :( ) 偶然发现了一些旧的代码,它们抛出并清空了一些强制转换异常(每次约20次:()

What if any is the performance hit were taking due to this? 如果由于这个原因,性能受到影响该怎么办? Should I be worried about this or is the overhead simply in the try / catch 我应该担心这个,还是只是在try / catch中的开销

Surprisingly lacking information on the topic of exception performance with C#. 令人惊讶的是,缺少有关C#异常性能主题的信息。

Thanks, from yours truly. 谢谢,真的,来自你的。

The exceptions are going to slow you down more than most average lines of code. 例外情况会比大多数平均代码行减慢速度。 Instead of casting and then catching the exception, do a check instead. 而不是强制转换然后捕获异常,而是进行检查。 For example 例如

BAD

myType foo = (myType)obj;
foo.ExecuteOperation();

GOOD

myType foo = obj as myType;
if (foo != null)
{
     foo.ExecuteOperation();
}

That's bad for two reasons. 这有两个原因。

  1. Exceptions are slow, there is quite a performance hit. 例外情况很慢,性能受到很大影响。 I don't think it'd take an entire millisecond as Matt pointed out, but they are slow enough that you want to avoid them in normal operation. Matt指出,我认为它不需要整整一毫秒,但它们足够慢,你想在正常操作中避免它们。
  2. Unless you have a good reason, you shouldn't catch empty exceptions. 除非你有充分的理由,否则你不应该抓住空的例外。 You're just hiding problems. 你只是隐藏了问题。 Better that a program crashes than that it carries on with potentially dangerous bugs. 程序崩溃比它继续发生的潜在危险错误更好。

If they're just try { } finally { } groups, then it's all good -- there's no overhead there. 如果他们只是try { } finally { }组,那么一切都很好 - 那里没有开销。 However, try { } catch { } is both potentially dangerous and potentially slow. 但是, try { } catch { }既有潜在危险又可能很慢。

As for documentation, this is pretty good: http://www.codeproject.com/KB/architecture/exceptionbestpractices.aspx#Don%27tuseexceptionhandlingasmeansofreturninginformationfromamethod18 至于文档,这是非常好的: http//www.codeproject.com/KB/architecture/exceptionbestpractices.aspx#Don%27tuseexceptionhandlingasmeansofreturninginformationfromamethod18

Edit: just realized you said empty catching exceptions, not catching empty exceptions. 编辑:刚刚意识到你说空捕捉异常,没有捕捉空异常。 Either way, unless you're dealing with IO, you probably want to avoid doing that for performance's sake. 无论哪种方式,除非您正在处理IO,否则您可能希望避免为了性能而这样做。

Exceptions are expensive, performance-wise. 例外是昂贵的,性能方面的。 Last time I measured these, they were taking about a full millisecond to throw and catch each exception. 上次我测量这些时,它们需要大约一整毫秒才能抛出并捕获每个异常。

Avoid using exceptions as a flow control mechanism. 避免使用异常作为流量控制机制。

As others have mentioned, Exceptions are expensive when thrown. 正如其他人所提到的,抛出时例外是昂贵的。 In some cases they can't be avoided. 在某些情况下,他们无法避免。

In this case, though, it sounds like they definitely can be. 但在这种情况下,听起来确实如此。

I would suggest using a the the as keyword before the cast. 我建议在演员表之前使用as关键字。 That will tell you if the cast succeeded or not thus avoiding the Exception altogether: 这将告诉你演员是否成功,从而完全避免了Exception:

object someObject;
SomeType typedObject;

// fill someObject

typedObject = someObject as SomeType;

if(typedObject == null)
{
    // Cast failed
}

If you haven't encountered any performance problem and this is the only way you have to do that algorithm continue to use this method. 如果您没有遇到任何性能问题,这是您必须执行该算法的唯一方法,请继续使用此方法。

Maybe before trying to cast you could see with some if clauses if you could do the cast. 也许在尝试演员之前你可以看到一些if条款,如果你可以做演员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM