我爲什麼不在“嘗試” - “捕獲”中包裹每個塊?

本文翻譯自:Why should I not wrap every block in “try”-“catch”?

I have always been of the belief that if a method can throw an exception then it is reckless not to protect this call with a meaningful try block. 我一直認爲,如果一個方法可以拋出一個異常,那麼不顧及用一個有意義的try塊來保護這個調用。

I just posted ' You should ALWAYS wrap calls that can throw in try, catch blocks. 我剛剛發佈了' 你應該總是包裝可以拋出try,catch塊的調用。 ' to this question and was told that it was 'remarkably bad advice' - I'd like to understand why. '對這個問題 ,並被告知這是'非常糟糕的建議' - 我想明白爲什麼。


#1樓

參考:https://stackoom.com/question/BU6S/我爲什麼不在-嘗試-捕獲-中包裹每個塊


#2樓

Besides the above advice, personally I use some try+catch+throw; 除了以上建議,我個人使用一些try + catch + throw; for the following reason: 原因如下:

  1. At boundary of different coder, I use try + catch + throw in the code written by myself, before the exception being thrown to the caller which is written by others, this gives me a chance to know some error condition occured in my code, and this place is much closer to the code which initially throw the exception, the closer, the easier to find the reason. 在不同編碼器的邊界,我在自己編寫的代碼中使用try + catch + throw,在異常被拋給其他人編寫的調用者之前,這讓我有機會知道我的代碼中出現的一些錯誤情況,以及這個地方更接近最初拋出異常的代碼,越接近,越容易找到原因。
  2. At the boundary of modules, although different module may be written my same person. 在模塊的邊界,雖然不同的模塊可能寫在我的同一個人。
  3. Learning + Debug purpose, in this case I use catch(...) in C++ and catch(Exception ex) in C#, for C++, the standard library does not throw too many exception, so this case is rare in C++. 學習+調試的目的,在這種情況下我在C ++中使用catch(...)並在C#中捕獲(Exception ex),對於C ++,標準庫不會拋出太多異常,所以這種情況在C ++中很少見。 But common place in C#, C# has a huge library and an mature exception hierarchy, the C# library code throw tons of exception, in theory I(and you) should know every exceptions from the function you called, and know the reason/case why these exception being thrown, and know how to handle them(pass by or catch and handle it in-place)gracefully. 但是在C#中常見的地方,C#有一個龐大的庫和一個成熟的異常層次結構,C#庫代碼拋出了大量的異常,理論上我(和你)應該知道你調用的函數的每個異常,並知道原因/案例爲什麼這些異常被拋出,並且知道如何處理它們(傳遞或捕獲並就地處理它)。 Unfortunately in reality it's very hard to know everything about the potential exceptions before I write one line of code. 不幸的是,實際上在編寫一行代碼之前很難知道有關潛在異常的所有內容。 So I catch all and let my code speak aloud by logging(in product environment)/assert dialog(in development environment) when any exception really occurs. 因此,當真正發生任何異常時,我會記錄(在產品環境中)/斷言對話框(在開發環境中),然後讓我的代碼大聲說話。 By this way I add exception handling code progressively. 通過這種方式,我逐步添加異常處理代碼。 I know it conflit with good advice but in reality it works for me and I don't know any better way for this problem. 我知道它有很好的建議,但實際上它適用於我,我不知道有任何更好的方法來解決這個問題。

#3樓

A method should only catch an exception when it can handle it in some sensible way. 一個方法應該只能在一些合理的方式處理它時捕獲異常。

Otherwise, pass it on up, in the hope that a method higher up the call stack can make sense of it. 否則,將其傳遞給up,希望調用堆棧上方的方法可以理解它。

As others have noted, it is good practice to have an unhandled exception handler (with logging) at the highest level of the call stack to ensure that any fatal errors are logged. 正如其他人所指出的那樣,最好在調用堆棧的最高級別擁有一個未處理的異常處理程序(帶有日誌記錄),以確保記錄任何致命錯誤。


#4樓

Because the next question is "I've caught an exception, what do I do next?" 因爲下一個問題是“我遇到了異常,我接下來該怎麼做?” What will you do? 你會怎麼做? If you do nothing - that's error hiding and the program could "just not work" without any chance to find what happened. 如果你什麼都不做 - 這就是隱藏的錯誤,程序可能“無法正常工作”而沒有任何機會找到發生的事情。 You need to understand what exactly you will do once you've caught the exception and only catch if you know. 一旦你發現異常,你需要了解你將會做什麼,如果你知道的話,你只能抓住。


#5樓

我聽過的最好的建議是你應該只在你可以明智地對異常情況做些什麼的時候捕捉異常,並且“捕獲,記錄和釋放”不是一個好的策略(如果在圖書館偶爾不可避免)。


#6樓

As stated in other answers, you should only catch an exception if you can do some sort of sensible error handling for it. 如其他答案中所述,如果您可以對其進行某種合理的錯誤處理,則應該只捕獲異常。

For example, in the question that spawned your question, the questioner asks whether it is safe to ignore exceptions for a lexical_cast from an integer to a string. 例如,在該問題的是催生了你的問題,提問者詢問是否可以安全地忽略一個例外lexical_cast從一個整數爲字符串。 Such a cast should never fail. 這樣的演員應該永遠不會失敗。 If it did fail, something has gone terribly wrong in the program. 如果確實失敗了,那麼程序就會出現嚴重錯誤。 What could you possibly do to recover in that situation? 在那種情況下你可以做些什麼來恢復? It's probably best to just let the program die, as it is in a state that can't be trusted. 最好讓程序死掉,因爲它處於一個無法信任的狀態。 So not handling the exception may be the safest thing to do. 因此,不處理異常可能是最安全的事情。

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