不抛出异常时,try/catch 块是否会影响性能? - Do try/catch blocks hurt performance when exceptions are not thrown?

问题:

During a code review with a Microsoft employee we came across a large section of code inside a try{} block.在与 Microsoft 员工进行代码审查期间,我们在try{}块中发现了一大段代码。 She and an IT representative suggested this can have effects on performance of the code.她和一位 IT 代表建议这会对代码的性能产生影响。 In fact, they suggested most of the code should be outside of try/catch blocks, and that only important sections should be checked.事实上,他们建议大部分代码应该在 try/catch 块之外,并且只应该检查重要的部分。 The Microsoft employee added and said an upcoming white paper warns against incorrect try/catch blocks.微软员工补充说,即将发布的白皮书警告不正确的 try/catch 块。

I've looked around and found it can affect optimizations , but it seems to only apply when a variable is shared between scopes.我环顾四周,发现它会影响优化,但它似乎只适用于在范围之间共享变量的情况。

I'm not asking about maintainability of the code, or even handling the right exceptions (the code in question needs re-factoring, no doubt).我不是在问代码的可维护性,甚至不是处理正确的异常(毫无疑问,有问题的代码需要重构)。 I'm also not referring to using exceptions for flow control, this is clearly wrong in most cases.我也不是指使用异常进行流量控制,这在大多数情况下显然是错误的。 Those are important issues (some are more important), but not the focus here.这些都是重要的问题(有些更重要),但不是这里的重点。

How do try/catch blocks affect performance when exceptions are not thrown?抛出异常时,try/catch 块如何影响性能?


解决方案:

参考一: https://stackoom.com/question/5UNk
参考二: Do try/catch blocks hurt performance when exceptions are not thrown?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章