使用C#中的异常。它会影响性能吗?

时间:2022-06-03 20:22:28

Basically, the question is: Do the Exceptions in C# affect the performance a lot? Is it better to avoid Exceptions rethrow? If i generate an exception in my code, does it affect a performance?

基本上,问题是:C#中的异常是否会影响性能?避免例外重新抛出是否更好?如果我在我的代码中生成异常,它是否会影响性能?

Sorry for the sillines of the question itself

对不起问题本身的sillines

8 个解决方案

#1


Microsoft's Design Guidelines for Developing Class Libraries is a very valuable resource. Here is a relevant article:

Microsoft的开发类库设计指南是一个非常有价值的资源。这是一篇相关的文章:

Exceptions and Performance

例外和表现

I would also recommend the Framework Design Guidelines book from Microsoft Press. It has a lot of the information from the Design Guidelines link, but it is annotated by people with MS, and Anders Hejlsberg, himself. It gives a lot of insight into the "why" and "how" of the way things are.

我还会推荐Microsoft Press的“框架设计指南”一书。它有很多来自设计指南链接的信息,但它由MS的人和Anders Hejlsberg本人注释。它提供了很多洞察事物的“为什么”和“如何”的方式。

#2


If you're worried about exception performance, you're using them wrong.

如果您担心异常性能,那么您使用它们是错误的。

But yes, exceptions do affect performance.

但是,是的,异常会影响性能。

#3


Raising an exception is an expensive operation in C# (compared to other operations in C#) but not enough that I would avoid doing it.

引发异常是C#中的一项昂贵的操作(与C#中的其他操作相比),但还不够,我会避免这样做。

I agree with Jared, if your application is significantly slower because of raising and throwing exceptions, I would take a look at your overall strategy. Something can probably be refactored to make exception handling more efficient rather than dismissing the concept of raising exceptions in code.

我同意Jared的意见,如果你的申请由于提出和抛出异常而显着变慢,我会看一下你的整体策略。有些东西可能会被重构,以使异常处理更有效,而不是忽略在代码中引发异常的概念。

#4


running code through a try/catch statement does not affect performance at all. The only performance hit comes if an exception is thrown ... because then the runtime has to unwind the stack and gather other information in order to populate the exception object.

通过try / catch语句运行代码根本不会影响性能。如果抛出异常,则唯一的性能影响是...因为运行时必须展开堆栈并收集其他信息以填充异常对象。

#5


What most other folks said, plus: Don't use exceptions as part of the programming flow. In other words, don't throw an exception for something like, account.withdrawalAmount > account.balance. That is a business case.

大多数人都说,加上:不要将异常作为编程流程的一部分。换句话说,不要为account.withdrawalAmount> account.balance之类的东西抛出异常。这是一个商业案例。

The other biggie to look out for regarding performance is swallowing exceptions. It's a slippery slope, and once you start allowing your app to swallow exceptions, you start doing it everywhere. Now you may be allowing your app to throw exceptions that you don't know about because you are swallowing them, your performance suffers and you don't know why.

关注绩效的另一个重要因素是吞噬异常。这是一个滑坡,一旦你开始允许你的应用程序吞下异常,你就开始在任何地方都这样做了。现在你可能会允许你的应用程序抛出你不知道的异常,因为你吞下它们,你的表现会受到影响,你不知道为什么。

#6


This is not silly just I've seen it somewhere else also on SO.

这不是愚蠢的,只是我已经在其他地方看到它了。

The exceptions occur well, when things are really exceptional. Most of the time you re-throw the exception (may after logging) when there are not many chances of recovering from it. So it should not bother you for normal course of execution of program.

当事情非常特殊时,异常就会发生。大多数情况下,当没有多少机会从中恢复时,您会重新抛出异常(可能在记录之后)。所以它不应该打扰你正常的程序执行过程。

#7


Exceptions as its name implies are intended to be exceptional. Hence you can't expect them to have been an important target for optimisation. More often then not they don't perform well since they have other priorites such as gathering detailed info about what went wrong.

顾名思义的例外情况是特殊的。因此,您不能指望它们是优化的重要目标。更常见的是,他们没有表现良好,因为他们有其他优先事项,如收集有关出错的详细信息。

#8


Exceptions in .NET do affect performance. This is the reason why they should be used only in exceptional cases.

.NET中的异常确实会影响性能。这就是为什么它们只应在特殊情况下使用的原因。

#1


Microsoft's Design Guidelines for Developing Class Libraries is a very valuable resource. Here is a relevant article:

Microsoft的开发类库设计指南是一个非常有价值的资源。这是一篇相关的文章:

Exceptions and Performance

例外和表现

I would also recommend the Framework Design Guidelines book from Microsoft Press. It has a lot of the information from the Design Guidelines link, but it is annotated by people with MS, and Anders Hejlsberg, himself. It gives a lot of insight into the "why" and "how" of the way things are.

我还会推荐Microsoft Press的“框架设计指南”一书。它有很多来自设计指南链接的信息,但它由MS的人和Anders Hejlsberg本人注释。它提供了很多洞察事物的“为什么”和“如何”的方式。

#2


If you're worried about exception performance, you're using them wrong.

如果您担心异常性能,那么您使用它们是错误的。

But yes, exceptions do affect performance.

但是,是的,异常会影响性能。

#3


Raising an exception is an expensive operation in C# (compared to other operations in C#) but not enough that I would avoid doing it.

引发异常是C#中的一项昂贵的操作(与C#中的其他操作相比),但还不够,我会避免这样做。

I agree with Jared, if your application is significantly slower because of raising and throwing exceptions, I would take a look at your overall strategy. Something can probably be refactored to make exception handling more efficient rather than dismissing the concept of raising exceptions in code.

我同意Jared的意见,如果你的申请由于提出和抛出异常而显着变慢,我会看一下你的整体策略。有些东西可能会被重构,以使异常处理更有效,而不是忽略在代码中引发异常的概念。

#4


running code through a try/catch statement does not affect performance at all. The only performance hit comes if an exception is thrown ... because then the runtime has to unwind the stack and gather other information in order to populate the exception object.

通过try / catch语句运行代码根本不会影响性能。如果抛出异常,则唯一的性能影响是...因为运行时必须展开堆栈并收集其他信息以填充异常对象。

#5


What most other folks said, plus: Don't use exceptions as part of the programming flow. In other words, don't throw an exception for something like, account.withdrawalAmount > account.balance. That is a business case.

大多数人都说,加上:不要将异常作为编程流程的一部分。换句话说,不要为account.withdrawalAmount> account.balance之类的东西抛出异常。这是一个商业案例。

The other biggie to look out for regarding performance is swallowing exceptions. It's a slippery slope, and once you start allowing your app to swallow exceptions, you start doing it everywhere. Now you may be allowing your app to throw exceptions that you don't know about because you are swallowing them, your performance suffers and you don't know why.

关注绩效的另一个重要因素是吞噬异常。这是一个滑坡,一旦你开始允许你的应用程序吞下异常,你就开始在任何地方都这样做了。现在你可能会允许你的应用程序抛出你不知道的异常,因为你吞下它们,你的表现会受到影响,你不知道为什么。

#6


This is not silly just I've seen it somewhere else also on SO.

这不是愚蠢的,只是我已经在其他地方看到它了。

The exceptions occur well, when things are really exceptional. Most of the time you re-throw the exception (may after logging) when there are not many chances of recovering from it. So it should not bother you for normal course of execution of program.

当事情非常特殊时,异常就会发生。大多数情况下,当没有多少机会从中恢复时,您会重新抛出异常(可能在记录之后)。所以它不应该打扰你正常的程序执行过程。

#7


Exceptions as its name implies are intended to be exceptional. Hence you can't expect them to have been an important target for optimisation. More often then not they don't perform well since they have other priorites such as gathering detailed info about what went wrong.

顾名思义的例外情况是特殊的。因此,您不能指望它们是优化的重要目标。更常见的是,他们没有表现良好,因为他们有其他优先事项,如收集有关出错的详细信息。

#8


Exceptions in .NET do affect performance. This is the reason why they should be used only in exceptional cases.

.NET中的异常确实会影响性能。这就是为什么它们只应在特殊情况下使用的原因。