为什么净程序员如此害怕的异常? [英] Why are .Net programmers so afraid of exceptions?

查看:234
本文介绍了为什么净程序员如此害怕的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我并不想成为任何人的关键在这里还是说所有的.NET程序员都这样。但我绝对注意到一个趋势:净程序员似乎避免例外像他们瘟疫

First of all, I'm not trying to be critical of anyone here or saying that all .Net programmers are this way. But I'm definitely noticing a trend: .Net programmers seem to avoid exceptions like they're the plague.

当然,总是有的雷蒙德陈博客张贴有关硬例外怎样才能处理。但是,这并不仅仅适用于.NET。

Of course, there's always the Raymond Chen blog post about how hard exceptions can be to handle. But that doesn't just apply to .Net.

在很多其他语言/环境,我没有注意到,程序员是可能避免的例外,因为他们在.NET。哎呀,在Python中,异常被抛出正常的程序执行(迭代器抛出StopIteration异常例外)。

In a lot of other languages/environments, I don't notice that programmers are as likely to avoid exceptions as they are in .Net. Heck, in Python, exceptions are thrown in normal program execution (iterators throw StopIteration exceptions).

那么,我在这里丢失?还是我完全关闭基地在想,.NET程序员避免比别人更多的异常?

So what am I missing here? Or am I totally off base in thinking that .net programmers avoid exceptions more than others?

推荐答案

.NET在很大程度上优化,允许的非特殊情况下尽可能快地运行,在异常性能为代价的。

.NET is heavily optimized to allow the non-exceptional case to run as fast as possible, at the expense of exception performance.

IronPython的比较CPython的

只有两个真正站出来为呈现出深沉的性能问题测试是两个异常处理的。这是唯一的测试,无论在哪里执行比其他快4倍以上。 IronPython的是10倍的try / catch语句快没有例外,CPython的是30倍的异常时,实际募集更快。这是.NET中的一个深思熟虑的设计决策,使代码不抛出异常运行得更快 - 即使这意味着放慢代码,不会抛出异常。我相当有信心,这是正确的决定 - 甚至记得那一天很久以前,当圭多正在讨论Python的例外制度,并解释说,他会接受几乎任何放缓的特殊情况,以换取从取出一条指令非特殊路径。

The only two tests that really stand out as showing a deep performance issue are the two exception handling ones. These are the only tests where either implementation is more than 4x faster than the other. IronPython is 10x faster on the try/catch without an exception and CPython is 30x faster when an exception is actually raised. This is a deliberate design decision within .NET to make code that doesn't throw exceptions run faster - even if that means slowing down code that does throw exceptions. I'm fairly confident this was the right decision - and even remember the day long ago when Guido was discussing Python's exception system and explained that he'd accept almost any slow-down to the exceptional case in return for removing a single instruction from the non-exceptional path.

这篇关于为什么净程序员如此害怕的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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