不拋出異常時,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?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章