过度使用try catch? [英] Overuse of try catch?

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

问题描述

我听到有人向我提到使用try catch异常处理

是非常昂贵的(相对于减慢应用程序的速度而言)如果使用的话

经常。当然他们无法解释原因。

这是真的吗?如果是这样,为什么?


谢谢。


STom

I heard someone mention to me that the use of try catch exception handling
is very expensive (in relative terms of slowing an app down) if it is used
frequently. Of course they could not explain why.

Is this true? If so, why?

Thanks.

STom

推荐答案

我听说过关于新错误处理的所有性能花絮都是抱怨抛出异常会浪费时间。


我想做这样的事情并没有太大的伤害,但确实是不必要的。如果i + = 1失败,机器有一个比

更大的问题,无论你的代码是否使用错误处理:


Dim i As Integer


尝试

i + = 1

抓住e作为例外

MessageBox.Show(" Bogus")

结束尝试


和任何工具箱一样,我会记住我应该使用

正确的工具这个工作,只有在实际需要的时候。


试试...... Catch是ADO.NET SqlConnection.Open()和其他的b / b $ b这样的东西需要什么失败的概率较高。你不想要

使用试试......抓住每一段代码。


只需我两美分。

-

Peace&快乐的计算,


Mike Labosh,MCSD MCT

所有者,vbSensei.Com

Escriba coda ergo sum。 - vbSensei
All of the performance tidbits I have heard about the new error handling are
complaining that Throwing exceptions waste time.

I imagine doing something like this doesn''t hurt [much], but is indeed
rather unneccesary. If i += 1 fails, the machine has a bigger problem than
whether or not your code uses error handling:

Dim i As Integer

Try
i += 1
Catch e As Exception
MessageBox.Show("Bogus")
End Try

As with any toolbox, I try to keep in mind that that I should use the
correct tool for the job, and only when actually needed.

Try...Catch is neccesary around an ADO.NET SqlConnection.Open() and other
such things that have a higher probability of failing. You wouldn''t want to
use Try...Catch around every single piece of code.

Just my two cents.
--
Peace & happy computing,

Mike Labosh, MCSD MCT
Owner, vbSensei.Com
"Escriba coda ergo sum." -- vbSensei


*" STom" < ST *********** @ hotmail.com> scripsit:
* "STom" <st***********@hotmail.com> scripsit:
我听到有人提到我使用try catch异常处理
非常昂贵(相对于减慢应用程序的速度)如果经常使用
。当然他们无法解释原因。
这是真的吗?如果是这样,为什么?
I heard someone mention to me that the use of try catch exception handling
is very expensive (in relative terms of slowing an app down) if it is used
frequently. Of course they could not explain why.

Is this true? If so, why?




查看异常类。一个''例外''存储了很多关于异常的信息,将抛出异常(这将是b / b
将控制权指向应用程序的其他部分)等对于

示例,使用一个用于表示成功/失败的布尔值,将会快速增加
的性能,但是您必须执行处理操作。你自己。


-

Herfried K. Wagner [MVP]

< http://www.mvps.org / dotnet>



Have a look at the ''Exception'' class. An ''Exception'' stores a lot of
information about the exception, the exception will be thrown (this will
direct the control to other parts of the application) etc. Using, for
example, a Boolean value for indicating success/failure, will increase
performance rapidely, but you will have to do the "handling" yourself.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>


是的,它是。例外是昂贵的,并且事先验证它们甚至更多。如果你真的想知道原因,请编译一个项目,并通过ILDASM.exe运行它来获得
。然后,添加一堆try catch块,看看生成多少代码
。你可能不熟悉IL,但它很漂亮

清楚。世界上应该被取缔的最糟糕的事情是On Error

Resume Next,它包装了try catch块中的每一行。考虑一下

a秒,如果你事先没有检查过任何东西,从文件的存在

到变量的存在,你不能写一个很少的代码(和一个

很多buggier?)嗯,这是由Try Catch放大它因为它执行b
的方式(不是一个完全类比,但是关闭)


无论如何,这里是交易......尝试捕获不应该在整个地方使用。

你应该只使用System.Exception在顶级调用b / c尝试Catch是

,以帮助您响应特定的异常,你当然不是
在每个块上写一个特殊的处理程序

实例的SystemOutOfMemoryException。


我想最好的类比方法就是这个。如果你开车的话,你需要你的车钥匙从办公室回家。

只是在你第一次上班时检查你的钥匙是否更有意义,然后当你准备离开时,你会花点b $ b b?比。每次检查你的钥匙

整天都在做什么?当你点击男人的房间时,你真的需要检查你的钥匙吗?或登录到计算机。或致电客户?不,它仍然很重要,但它与手头的任务无关。它不会因为你的工作而停止工作,但是它会减慢它的速度,并且可能会因此而导致性能下降,因此它会阻碍性能。


HTH,


Bill

" STom" < ST *********** @ hotmail.com>在留言中写道

news:%2 **************** @ TK2MSFTNGP09.phx.gbl ...
Yes, it is. Exceptions are costly and verifying them before hand is even
moreso. If you really want to know why, compile a project, and run it
through ILDASM.exe. Then, add a bunch of try catch blocks and see how much
more code is generated. You may not be familiar with IL, but it''s pretty
clear. And the worst thing in the World that should be outlawed is On Error
Resume Next, that wraps every line in a try catch block. Think about it for
a second, if you never checked anything beforehand, from a file''s existence
to the existence of a variable, couldn''t you write a lot less code (and a
lot buggier?) Well, this is magnified by Try Catch becuase of the way it
executes (not an exact analogy, but close)

Anyway, here''s the deal...Try catch shouldn''t be used all over the place.
You should only use System.Exception at top level calls b/c Try Catch is
made to help you respond to specific exceptions, and you certainly aren''t
writng a special handler at every block for a SystemOutOfMemoryException for
instance.

I guess the best way to analogize it is this. You need your car keys to get
home from the office if you are driving. Wouldn''t it make more sense to
only check that you have your keys when you first leave for work, and then
when you are ready to leave? Vs. Checking for your keys every time you do
anything all day? Do you really need to check for your keys when you hit
the men''s room? Or log onto a computer. Or call a client? No. It''s still
important, but it has nothign to do with the task at hand. It won''t stop
you from doing your job, but it will slow it down, and possibly so much so
that it would impede performance..

HTH,

Bill
"STom" <st***********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
我听说有人提到我,如果频繁使用,那么使用try catch异常处理是非常昂贵的(相对于减慢app的速度)。当然他们无法解释原因。
这是真的吗?如果是这样,为什么?

谢谢。

STom
I heard someone mention to me that the use of try catch exception handling
is very expensive (in relative terms of slowing an app down) if it is used
frequently. Of course they could not explain why.

Is this true? If so, why?

Thanks.

STom



这篇关于过度使用try catch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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