你(真的)編寫異常安全代碼嗎? [關閉]

本文翻譯自:Do you (really) write exception safe code? [closed]

Exception handling (EH) seems to be the current standard, and by searching the web, I can not find any novel ideas or methods that try to improve or replace it (well, some variations exist, but nothing novel). 異常處理(EH)似乎是當前的標準,並且通過搜索網絡,我找不到任何試圖改進或替換它的新穎想法或方法(好吧,存在一些變化,但沒有新穎的)。

Though most people seem to ignore it or just accept it, EH has some huge drawbacks: exceptions are invisible to the code and it creates many, many possible exit points. 雖然大多數人似乎忽略它或只是接受它,但EH 一些巨大的缺點:代碼看不到異常,它會創建許多可能的退出點。 Joel on software wrote an article about it . 喬爾在軟件上寫了一篇關於它文章 The comparison to goto fits perfect, it made me think again about EH. goto的比較非常完美,它讓我再次想到了EH。

I try to avoid EH and just use return values, callbacks or whatever fits the purpose. 我儘量避免使用EH,只使用返回值,回調或任何適合目的的東西。 But when you have to write reliable code, you just can't ignore EH these days : It starts with the new , which may throw an exception, instead of just returning 0 (like in the old days). 但是當你必須編寫可靠的代碼時,你現在就不能忽視EH :它從new開始,它可能拋出一個異常,而不是隻返回0(就像過去一樣)。 This makes about any line of C++ code vulnerable to an exception. 這使得任何C ++代碼行都容易受到異常的影響。 And then more places in the C++ foundational code throw exceptions... std lib does it, and so on. 然後C ++基礎代碼中的更多地方拋出異常...... std lib執行它,依此類推。

This feels like walking on shaky grounds .. So, now we are forced to take care about exceptions! 這感覺就像走在搖搖欲墜的地面上 。所以,現在我們被迫關注異常!

But its hard, its really hard. 但它很難,真的很難。 You have to learn to write exception safe code, and even if you have some experience with it, it will still be required to double check any single line of code to be safe! 你必須學會​​編寫異常安全代碼,即使你有一些經驗,它仍然需要仔細檢查任何一行代碼是安全的! Or you start to put try/catch blocks everywhere, which clutters the code until it reaches a state of unreadability. 或者你開始在任何地方放置try / catch塊,這會使代碼混亂,直到它達到不可讀狀態。

EH replaced the old clean deterministical approach (return values..), which had just a few but understandable and easily solveable drawbacks with an approach that creates many possible exit points in your code, and if you start writing code that catches exceptions (what you are forced to do at some point), then it even creates a multitude of paths through your code (code in the catch blocks, think about a server program where you need logging facilities other than std::cerr ..). EH取代了舊的乾淨確定性方法(返回值..),它只有一些但可以理解且易於解決的缺點,一種方法可以在代碼中創建許多可能的退出點,並且如果你開始編寫捕獲異常的代碼(你是什麼的)在某些時候被迫做某事),然後它甚至通過你的代碼創建了許多路徑(catch塊中的代碼,考慮一個服務器程序,你需要除了std :: cerr之外的日誌工具..)。 EH has advantages, but that's not the point. EH有優勢,但這不是重點。

My actual questions: 我的實際問題:

  • Do you really write exception safe code? 你真的寫異常安全代碼嗎?
  • Are you sure your last "production ready" code is exception safe? 您確定最後一個“生產就緒”代碼是異常安全的嗎?
  • Can you even be sure, that it is? 你能確定嗎,它是嗎?
  • Do you know and/or actually use alternatives that work? 你知道和/或實際使用有效的替代品嗎?

#1樓

參考:https://stackoom.com/question/7m71/你-真的-編寫異常安全代碼嗎-關閉


#2樓

Some of us have been using exception for over 20 years. 我們中的一些人已經使用例外超過20年。 PL/I has them, for example. 例如,PL / I有它們。 The premise that they are a new and dangerous technology seems questionable to me. 它們是一種新的危險技術的前提似乎對我來說是個問題。


#3樓

  • Do you really write exception safe code? 你真的寫異常安全代碼嗎?

Well, I certainly intend to. 好吧,我當然打算。

  • Are you sure your last "production ready" code is exception safe? 您確定最後一個“生產就緒”代碼是異常安全的嗎?

I'm sure that my 24/7 servers built using exceptions run 24/7 and don't leak memory. 我確信使用異常構建的24/7服務器可以全天候運行並且不會泄漏內存。

  • Can you even be sure, that it is? 你能確定嗎,它是嗎?

It's very difficult to be sure that any code is correct. 很難確定任何代碼是否正確。 Typically, one can only go by results 通常,人們只能按結果去做

  • Do you know and/or actually use alternatives that work? 你知道和/或實際使用有效的替代品嗎?

No. Using exceptions is cleaner and easier than any of the alternatives I've used over the last 30 years in programming. 沒有。使用例外比我在編程過去30年中使用的任何替代方案更清晰,更容易。


#4樓

Leaving aside the confusion between SEH and C++ exceptions, you need to be aware that exceptions can be thrown at any time, and write your code with that in mind. 拋開SEH和C ++異常之間的混淆,您需要意識到可以隨時拋出異常,並在編寫代碼時考慮到這一點。 The need for exception-safety is largely what drives the use of RAII, smart pointers, and other modern C++ techniques. 對異常安全的需求在很大程度上推動了RAII,智能指針和其他現代C ++技術的使用。

If you follow the well-established patterns, writing exception-safe code is not particularly hard, and in fact it's easier than writing code that handles error returns properly in all cases. 如果你遵循完善的模式,編寫異常安全的代碼並不是特別困難,事實上它比編寫在所有情況下都能正確處理錯誤返回的代碼更容易。


#5樓

I really like working with Eclipse and Java though (new to Java), because it throws errors in the editor if you are missing an EH handler. 我非常喜歡使用Eclipse和Java(Java新手),因爲如果你缺少一個EH處理程序,它會在編輯器中拋出錯誤。 That makes things a LOT harder to forget to handle an exception... 這使得忘記處理異常變得更加困難......

Plus, with the IDE tools, it adds the try / catch block or another catch block automatically. 此外,使用IDE工具,它會自動添加try / catch塊或其他catch塊。


#6樓

Some of us prefer languages like Java which force us to declare all the exceptions thrown by methods, instead of making them invisible as in C++ and C#. 我們中的一些人更喜歡像Java這樣的語言,它迫使我們聲明方法拋出的所有異常,而不是像C ++和C#那樣使它們不可見。

When done properly, exceptions are superior to error return codes, if for no other reason than you don't have to propagate failures up the call chain manually. 如果正確完成,異常優於錯誤返回代碼,如果沒有其他原因,您不必手動向上傳播失敗的調用鏈。

That being said, low-level API library programming should probably avoid exception handling, and stick to error return codes. 話雖這麼說,低級API庫編程應該可以避免異常處理,並堅持錯誤返回代碼。

It's been my experience that it's difficult to write clean exception handling code in C++. 根據我的經驗,用C ++編寫乾淨的異常處理代碼很困難。 I end up using new(nothrow) a lot. 我最終使用了new(nothrow)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章