什么是try / catch语句在C#中的实际开销?

问题描述:

所以,我知道的try / catch确实增加了一些开销,因此不控制流程的一个很好的方式,但如果没有这方面的开销从何而来,什么是它的实际影响?

So, I know that try/catch does add some overhead and therefore isn't a good way of controlling process flow, but where does this overhead come from and what is it's actual impact?

我不是语言实现的专家(所以借此与一粒盐),但我觉得最大的成本之一是展开堆栈和将其存储在堆栈跟踪。我怀疑这种情况下,只有当异常被抛出(但我不知道),如果是的话,这将是体面地抛出一个异常每次大小隐性成本......所以它不是像你只是从一个地方跳到在code到另一个,有很多事情。

I'm not an expert in language implementations (so take this with a grain of salt), but I think one of the biggest costs is unwinding the stack and storing it for the stack trace. I suspect this happens only when the exception is thrown (but I don't know), and if so, this would be decently sized hidden cost every time an exception is thrown... so it's not like you are just jumping from one place in the code to another, there is a lot going on.

我不认为这是一个问题,只要您使用的异常行为异常(所以不是典型的,通过该计划预期路径)。

I don't think it's a problem as long as you are using exceptions for EXCEPTIONAL behavior (so not your typical, expected path through the program).