什么是try / catch语句在C#中的实际开销? [英] What is the real overhead of try/catch in C#?

查看:393
本文介绍了什么是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).

这篇关于什么是try / catch语句在C#中的实际开销?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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