[Semi-OT]关于异常处理 [英] [Semi-OT] About Exception Handling

查看:52
本文介绍了[Semi-OT]关于异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前段时间,我记得在阅读有关优势的讨论以及异常处理的弱点。与正常相比,异常处理效率低(使用CPU的方式为
)是一个缺点。练习返回值。


这是真的吗?通常情况下,使用异常处理是否会比返回值效率低得多,或效率低一点?


只是好奇......


TIA

Some time ago, I remember reading a discussion about the strengths and
weaknesses of exception handling. One of the weaknesses that was put
forward was that exception handling is inefficient (in the way of CPU
usage), compared to the "normal" practise returning values.

How true is this? Will using using exception handling, in general, be
much less efficient than returning values, or less efficient at all?

Just curious...

TIA

推荐答案

" C#Learner" < CS **** @ learner.here>在消息中写道

news:go ******************************** @ 4ax.com ...
"C# Learner" <cs****@learner.here> wrote in message
news:go********************************@4ax.com...
前段时间,我还记得有关异常处理的优势和缺点的讨论。向前推进的一个弱点是,与正常相比,异常处理效率低(以CPU的使用方式)。练习回归价值。

这是真的吗?一般来说,使用异常处理是否会比返回值效率低得多,或效率更低?
Some time ago, I remember reading a discussion about the strengths and
weaknesses of exception handling. One of the weaknesses that was put
forward was that exception handling is inefficient (in the way of CPU
usage), compared to the "normal" practise returning values.

How true is this? Will using using exception handling, in general, be
much less efficient than returning values, or less efficient at all?




C#Learner,

在C#中,异常处理不会花费任何费用,直到异常是实际提出的
。虽然性能受到打击,但通常并不足以让人关心,并且其好处超过了返回错误值的旧方法的缺点。

返回错误值的旧方法。 >

当您使用异常处理时,您可以获得有关

在您的应用程序中发生的更多信息。只要想想基本的Exception

对象,就会有一条消息,StackTrace和其他属性,可以让你对引发异常时发生的事情有很多了解。

此外,当BCL附带的众多

例外之一无法满足您的需求时,您可以定义自己的自定义例外。当一个

异常被提出时,你不能忽视或忘记它,因为它在你的脸上是

。你还可以更灵活地处理

异常,而不是方法的结果,堆栈展开,

未处理的异常处理程序,以及类似的酷东西。


Joe

-
http://www.csharp-station.com



Hi C# Learner,

In C#, exception handling doesn''t cost you anything until an exception is
actually raised. While there is a performance hit, it is generally not
significant enough to care about and the benefits outweigh the drawbacks of
the old method of returning error values.

When you use exception handling, you gain a lot more information about what
is happening in your application. Just think about the basic Exception
object, there is a message, StackTrace, and other properties that give you a
lot of insight into what happened when the exception was raised.
Additionally, you can define your own custom exceptions when one of the many
exceptions that ship with the BCL doesn''t meet your needs. When an
exception is raised, you can''t ignore or forget about it because it is in
your face. You also have much more flexibility in how you handle
exceptions, as opposed to results from a method, with stack unwinding,
unhandled exception handlers, and cool stuff like that.

Joe
--
http://www.csharp-station.com


正如Joe Mayo所提到的,异常并非都是例外只要没有实际抛出异常,效率就会很低。但是,您应该假设抛出异常非常昂贵。从本质上讲,您不应该使用异常来进行正常的控制流程。仅在真正特殊情况下抛出异常 - 在实际程序运行中应该很少或永远不会出现的情况。如果你经常抛出和捕捉异常,那不仅是糟糕的风格,而且还可能是非常严重的性能打击


- Ada


----- C#Learner写道:----


前段时间,我记得读过有关优势的讨论

异常处理的弱点。其中一个缺点是,与正常相比,异常处理效率低(使用CP的方式为
)。练习回归价值


这是真的吗?一般来说,使用异常处理会比返回值效率低得多,或者效率低一点


只是好奇..


TI

As Joe Mayo mentioned, exceptions aren''t all that inefficient as long as no exceptions are actually thrown. However, you should assume that throwing exceptions is very expensive. Essentially, you should never use exceptions for normal control flow. Throw exceptions only in truly exceptional cases -- cases that should rarely or never come up in actual program runs. If you''re throwing and catching exceptions often, it''s not only bad style, but can also be a pretty severe performance hit

-- Ada

----- C# Learner wrote: ----

Some time ago, I remember reading a discussion about the strengths an
weaknesses of exception handling. One of the weaknesses that was pu
forward was that exception handling is inefficient (in the way of CP
usage), compared to the "normal" practise returning values

How true is this? Will using using exception handling, in general, b
much less efficient than returning values, or less efficient at all

Just curious..

TI


我同意你的意见,以避免正常控制流程的例外,但它是

似乎没有其他方法来处理.Net中的某些事情


示例:

如何检测BinaryReader的流结束?可能会有更好的方式,但这是我现在找到的最有效的方式。


========== =开始代码===========

BinaryReader br = new BinaryReader(File.OpenRead(@" c:\ test.txt"));


while(true)

{

try

{

byte current = br.ReadByte();

}

catch(EndOfStreamException e)

{

//结束检测到流,时间退出循环

休息;

}

}

== =========结束代码===========

Yves


" AdamMil" ; <一个******* @ discussions.microsoft.com> schreef in bericht

news:35 ********************************** @ microsof t.com ...
I agree with your point to avoid exceptions for normal control flow but it
seems like there''s no other way to handle some things in .Net

Example :
How do you detect the end of stream of a BinaryReader? There might be a
better way but that''s the most efficient one I found for now.

=========== start code ===========
BinaryReader br = new BinaryReader(File.OpenRead(@"c:\test.txt"));

while (true)
{
try
{
byte current = br.ReadByte();
}
catch (EndOfStreamException e)
{
// The end of the stream was detected, time to get out of the loop
break;
}
}
=========== end code ===========

Yves

"AdamMil" <an*******@discussions.microsoft.com> schreef in bericht
news:35**********************************@microsof t.com...
正如Joe Mayo所提到的,只要
没有异常实际抛出,异常就不会那么低效。但是,您应该假设抛出

异常是非常昂贵的。基本上,你不应该使用例外

来进行正常的控制流程。仅在真正特殊情况下抛出异常 -

在实际程序运行中应该很少或永远不会出现的情况。如果你经常抛出和捕捉异常,那不仅是糟糕的风格,而且还可以是非常严重的性能损失。
- Adam

----- C#Learner写道:-----

前段时间,我记得读过有关优势
和异常处理的弱点。向前推进的一个弱点是,与正常相比,异常处理效率低(以CPU的使用方式)。练习回归价值。

这是真的吗?一般来说,使用异常处理是否会使
的效率低于返回值,或者效率会低一些?

只是好奇......

TIA
As Joe Mayo mentioned, exceptions aren''t all that inefficient as long as no exceptions are actually thrown. However, you should assume that throwing
exceptions is very expensive. Essentially, you should never use exceptions
for normal control flow. Throw exceptions only in truly exceptional cases --
cases that should rarely or never come up in actual program runs. If you''re
throwing and catching exceptions often, it''s not only bad style, but can
also be a pretty severe performance hit.
-- Adam

----- C# Learner wrote: -----

Some time ago, I remember reading a discussion about the strengths and weaknesses of exception handling. One of the weaknesses that was put
forward was that exception handling is inefficient (in the way of CPU
usage), compared to the "normal" practise returning values.

How true is this? Will using using exception handling, in general, be much less efficient than returning values, or less efficient at all?

Just curious...

TIA



这篇关于[Semi-OT]关于异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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