rcu寬限期_如何處理寬限期錯誤:靜默失敗不是一種選擇

rcu寬限期

by Rina Artstain

通過麗娜·阿斯特斯坦

I’ve never really had much of an opinion about error handling. This may come as a shock to people who know me as quite opinionated (in a good way!), but yeah. If I was coming into an existing code base I just did whatever they did before, and if I was writing from scratch I just did whatever felt right to me at the time.

對于錯誤處理,我從來沒有真正有過很多意見。 這可能會令那些了解我的人震驚(以一種很好的方式!),但是是的。 如果我要使用現有的代碼庫,那么我只是做他們以前做過的事情,如果我是從頭開始寫的,那么我當時所做的一切都是對的。

When I recently read the error handling section in Clean Code by Uncle Bob, that was the first time I gave the subject any thought at all. Sometime later I ran into a bug which was caused by some code failing silently, and I realized it might be time to think about it a bit more. I might not be able to change how errors are handled in the entire code base I’m working on, but at least I would be informed on what approaches exists and what the tradeoffs are, and, you know, have an opinion about the matter.

當我最近閱讀Bob叔叔的“ 清理代碼”中的錯誤處理部分時,那是我第一次對主題有任何想法。 以后的某個時候,我遇到了一個錯誤,該錯誤是由一些代碼無聲失敗導致的,我意識到可能應該再考慮一下。 我可能無法更改正在處理的整個代碼庫中錯誤的處理方式,但是至少我會被告知存在哪些方法以及如何權衡,并且您對此事有意見。

期待西班牙宗教裁判所 (Expect the Spanish Inquisition)

The first step of handling errors is to identify when an “error” is not an “error!”. This of course depends on your application’s business logic, but in general, some errors are obvious and easy to fix.

處理錯誤的第一步是確定何時“錯誤”不是“錯誤!”。 當然,這取決于應用程序的業務邏輯,但是總的來說,有些錯誤是顯而易見的,并且易于修復。

  • Got a from-to date range where the “to” is before “from”? Switch the order.

    是否有一個從“到”在“從”之前的“到”日期范圍? 切換順序。
  • Got a phone number which starts with + or contains dashes where you expect no special characters? Remove them.

    是否有一個以+開頭或包含破折號的電話號碼,而您不希望這些字符帶有特殊字符? 刪除它們。
  • Null collection a problem? Make sure you initialize it before accessing (using lazy initialization or in the constructor).

    空集合有問題嗎? 確保在訪問之前將其初始化(使用惰性初始化或在構造函數中)。

Don’t interrupt your code flow for errors you can fix, and certainly don’t interrupt your users. If you can understand the problem and fix it yourself — just do it.

不要因為可以解決的錯誤而中斷代碼流,當然也不要中斷用戶。 如果您可以理解問題并親自解決,請執行此操作。

返回Null或其他魔術值 (Returning Null or Other Magic Values)

Null values, -1 where a positive number is expected and other “magic” return values — all these are evils which move the responsibility for error checking to the caller of the function. This is not only a problem because it causes error checking to proliferate and multiply, it is also a problem because it depends on convention and requires your user to be aware of arbitrary implementation details.

空值-1(期望為正數)和其他“魔術”返回值-所有這些都是罪惡,將錯誤檢查的責任移交給了函數的調用者。 這不僅是一個問題,因為它會導致錯誤檢查激增并成倍增加,它也是一個問題,因為它依賴于約定并且要求您的用戶注意任意實現的細節。

Your code will be full of code blocks like these which obscure the application’s logic:

您的代碼將充滿像這樣的代碼塊,這些代碼塊會模糊應用程序的邏輯:

return_value = possibly_return_a_magic_value()if return_value < 0:   handle_error()else:    do_something()
other_return_value = possibly_nullable_value()if other_return_value is None:   handle_null_value()else:   do_some_other_thing()

Even if your language has a built in nullable value propagation system — that’s just applying an unreadable patch to flaky code:

即使您的語言具有內置的可為空的值傳播系統,也只是對易碎的代碼應用了不可讀的補丁:

var item = returnSomethingWhichCouldBeNull();var result = item?.Property?.MaybeExists;if (result.HasValue){    DoSomething();}
Passing null values to methods is just as problematic, and you’ll often see methods begin with a few lines of checking that the input is valid, but this is truly unnecessary. Most modern languages provide several tools which allow you to be explicit about what you expect and skip those code-cluttering checks, e.g. defining parameters as non-nullable or with an appropriate decorator.
將null值傳遞給方法同樣存在問題,并且您經常會看到方法從檢查輸入是否有效的幾行開始,但這確實是不必要的。 大多數現代語言提供了幾種工具,使您可以清楚地了解期望的內容并跳過那些代碼混亂的檢查,例如,將參數定義為不可為空或使用適當的修飾符。

錯誤代碼 (Error Codes)

Error codes have the same problem as null and other magic values, with the additional complication of having to, well, deal with error codes.

錯誤代碼與null和其他魔術值具有相同的問題,另外還必須處理錯誤代碼。

You might decide to return the error code through an “out” parameter:

您可能決定通過“ out”參數返回錯誤代碼:

int errorCode;var result = getSomething(out errorCode);if (errorCode != 0){    doSomethingWithResult(result);}

You may choose to wrap all your results in a “Result” construct like this (I’m very guilty of this one, though it was very useful for ajax calls at the time):

您可以選擇將所有結果包裝在這樣的“結果”構造中(我對此非常內,,盡管當時對ajax調用非常有用):

public class Result<T>{   public T Item { get; set; }   // At least "ErrorCode" is an enum   public ErrorCode ErrorCode { get; set; } = ErrorCode.None;    public IsError { get { return ErrorCode != ErrorCode.None; } } }
public class UsingResultConstruct{   ...   var result = GetResult();   if (result.IsError)   {      switch (result.ErrorCode)      {         case ErrorCode.NetworkError:             HandleNetworkError();             break;         case ErrorCode.UserError:             HandleUserError();             break;         default:             HandleUnknownError();             break;      }   }   ActuallyDoSomethingWithResult(result);   ...}

Yep. That’s really bad. The Item property could still be empty for some reason, there’s no actual guarantee (besides convention) that when the result doesn’t contain an error you can safely access the Item property.

是的 真的很糟糕 由于某些原因,Item屬性仍可能為空,沒有任何實際保證(約定除外),即當結果不包含錯誤時,您可以安全地訪問Item屬性。

After you’re done with all of this handling, you still have to translate your error code to an error message and do something with it. Often, at this point you’ve obscured the original problem enough that you might not have the exact details of what happened, so you can’t even report the error effectively.

完成所有這些處理后,您仍然必須將錯誤代碼轉換為錯誤消息并對其進行處理。 通常,在這一點上,您已經掩蓋了最初的問題,以至于您可能不知道所發生的事情的確切細節,因此甚至無法有效地報告錯誤。

On top of this horribly unnecessarily over-complicated and unreadable code, an even worse problem exists — if you, or someone else, change your internal implementation to handle a new invalid state with a new error code, the calling code will have no way of knowing something which they need to handle has changed and will fail in unpredictable ways.

在此可怕不必要過于復雜而無法讀取的代碼頂部,一個更糟糕的問題存在-如果你或其他人,改變你的內部實現來處理與新的錯誤代碼的新無效狀態,調用代碼將沒有任何的辦法知道他們需要處理的事情已經改變,并且將以無法預測的方式失敗

如果一開始您沒有成功,請嘗試,然后再抓住 (If At First You Don’t Succeed, Try, Catch, Finally)

Before we continue, this might be a good time to mention that code failing silently is not a good thing. Failing silently means errors can go undetected for quite a while before exploding suddenly at inconvenient and unpredictable times. Usually over the weekend. The previous error handling methods allow you to fail silently, so maybe, just maybe, they’re not the best way to go.

在繼續之前,這可能是一個很好的時機,指出代碼無聲失敗不是一件好事。 靜默失敗意味著很長一段時間都無法發現錯誤,然后在不方便且不可預測的時間突然爆炸。 通常在周末。 先前的錯誤處理方法使您能夠靜默地失敗,所以也許,也許不是最好的方法。

At this point, if you’ve read Clean Code you’re probably wondering why anyone would ever do any of that instead of just throwing an exception? If you haven’t, you might think exceptions are the root of all evil. I used to feel the same way, but now I’m not so sure. Bear with me, let’s see if we can agree that exceptions are not all bad, and might even be quite useful. And if you’re writing in a language without exceptions? Well, it is what it is.

在這一點上,如果您已經閱讀了Clean Code,您可能想知道為什么有人會這樣做而不是僅僅引發異常? 如果沒有,您可能會認為例外是萬惡之源。 我曾經有過同樣的感覺,但是現在我不太確定。 忍受我,讓我們看看我們是否可以同意例外并非全都不好,甚至可能非常有用。 而且,如果您使用的語言無一例外? 好吧,就是這樣。

An interesting side note, at least to me, is that the default implementation for a new C# method is to throw a NotImplementedException, whereas the default for a new python method is “pass”.

至少對我而言,一個有趣的旁注是,新C#方法的默認實現是拋出NotImplementedException,而新python方法的默認實現是“ pass”。

I’m not sure if this is a C# convention or just how my Resharper was configured, but the result is basically setting up python to fail silently. I wonder how many developers have spent a long and sad debugging session trying to figure what was going on, only to find out they had forgotten to implement a placeholder method.

我不確定這是C#約定還是Resharper的配置方式,但結果基本上是將python設置為靜默失敗。 我想知道有多少開發人員花了很長時間來進行調試,以弄清楚到底發生了什么,卻發現他們忘記了實現占位符方法。

But wait, you could easily create a cluttered mess of error checking and exception throwing which is quite similar to the previous error checking sections!

但是,等等,您可以輕松創建混亂的錯誤檢查和異常拋出,這與前面的錯誤檢查部分非常相似!

public MyDataObject UpdateSomething(MyDataObject toUpdate){    if (_dbConnection == null)    {         throw new DbConnectionError();    }    try    {        var newVersion = _dbConnection.Update(toUpdate);        if (newVersion == null)        {            return null;        }        MyDataObject result = new MyDataObject(newVersion);        return result;     }     catch (DbConnectionClosedException dbcc)     {         throw new DbConnectionError();     }     catch (MyDataObjectUnhappyException dou)     {         throw new MalformedDataException();     }     catch (Exception ex)     {         throw new UnknownErrorException();     }}

So, of course, throwing exceptions will not protect you from unreadable and unmanageable code. You need to apply exception throwing as a well thought out strategy. If your scope is too big, your application might end up in an inconsistent state. If your scope is too small, you’ll end up with a cluttered mess.

因此,當然,拋出異常不會保護您免受無法閱讀和無法管理的代碼的侵害。 您需要將異常拋出作為一種經過深思熟慮的策略。 如果范圍太大,則應用程序可能會處于不一致狀態。 如果范圍太小,您將陷入混亂。

My approach to this problem is as follows:

我對這個問題的解決方法如下:

Consistency rulezzz. You must make sure that your application is always in a consistent state. Ugly code makes me sad, but not as much as actual problems which affect the users of whatever it is your code is actually doing. If that means you have to wrap every couple of lines with a try/catch block — hide them inside another function.

一致性rulezzz。 您必須確保您的應用程序始終處于一致狀態。 丑陋的代碼讓我很傷心,但不像實際問題那樣嚴重,這些問題會影響用戶的實際行為。 如果這意味著您必須用try / catch塊包裝每兩行,請將它們隱藏在另一個函數中。

def my_function():    try:        do_this()        do_that()    except:        something_bad_happened()    finally:        cleanup_resource()

Consolidate errors. It’s fine if you care about different kinds of errors which need to be handled differently, but do your users a favor and hide that internally. Externally, throw a single type of exception just to let your users know something went wrong. They shouldn’t really care about the details, that’s your responsibility.

合并錯誤。 如果關心需要以不同方式處理的不同類型的錯誤,但對您的用戶有所幫助并在內部隱藏該錯誤,那是很好的。 在外部,僅引發一種異常即可讓您的用戶知道出了什么問題。 他們不應該真正在乎細節,這是您的責任。

public MyDataObject UpdateSomething(MyDataObject toUpdate){    try    {                var newVersion = _dbConnection.Update(toUpdate);        MyDataObject result = new MyDataObject(newVersion);        return result;     }     catch (DbConnectionClosedException dbcc)     {         HandleDbConnectionClosed();         throw new UpdateMyDataObjectException();     }     catch (MyDataObjectUnhappyException dou)     {         RollbackVersion();         throw new UpdateMyDataObjectException();     }     catch (Exception ex)     {         throw new UpdateMyDataObjectException();     }}

Catch early, catch often. Catch your exceptions as close to the source at the lowest level possible. Maintain consistency and hide the details (as explained above), then try to avoid handling errors until the very top level of your application. Hopefully there aren’t too many levels along the way. If you can pull this off, you’ll be able to clearly separate the normal flow of your application logic from the error handling flow, allowing your code to be clear and concise without mixing concerns.

早點捕獲,經常捕獲。 在可能的最低級別上將異常捕獲到盡可能接近源的位置。 保持一致性并隱藏細節(??如上所述),然后嘗試避免錯誤直到應用程序的最高層。 希望在此過程中不要有太多的級別。 如果可以做到這一點,您將能夠清楚地將應用程序邏輯的正常流程與錯誤處理流程區分開,從而使您的代碼清晰明了,而無需擔心。

def my_api():    try:        item = get_something_from_the_db()        new_version = do_something_to_item(item)        return new_version    except Exception as ex:        handle_high_level_exception(ex)

Thanks for reading this far, I hope it was helpful! Also, I’m only starting to form my opinions on this subject, so I’d be really happy to hear what your strategy is for handling errors. The comments section is open!

感謝您閱讀本文,希望對您有所幫助! 另外,我只是開始就此問題發表意見,因此,我很高興聽到您處理錯誤的策略。 評論部分已打開!

翻譯自: https://www.freecodecamp.org/news/how-to-handle-errors-with-grace-failing-silently-is-not-an-option-de6ce8f897d7/

rcu寬限期

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392248.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392248.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392248.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

描述符、迭代器、生成器

描述符&#xff1a;將某種特殊類型的類的實例指派給另一個類的屬性。 此處特殊類型的要求&#xff0c;至少實現”__set__(self , instance , owner)“、”__get__(self , instance , value)“、”__delete__(self , instance )“三個方法中的一個。 >>> class MyDecri…

php模擬表單提交登錄,PHP模擬表單的post請求實現登錄

stuid > $stuid,pwd > $pwd);$ch curl_init (); //初始化curlcurl_setopt ( $ch, CURLOPT_URL, $uri );curl_setopt ( $ch, CURLOPT_POST, 1 ); //使用post請求curl_setopt ( $ch, CURLOPT_HEADER, 0 );curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );curl_setopt ( $…

去除list集合中重復項的幾種方法

因為用到list&#xff0c;要去除重復數據&#xff0c;嘗試了幾種方法。記錄于此。。。 測試數據&#xff1a; List<string> li1 new List<string> { "8", "8", "9", "9" ,"0","9"};List<string&g…

Crystal Reports第一張報表

新建一個網站項目&#xff0c;1. 設置數據庫 從服務器資源管理器中&#xff0c;數據連接中添加新連接&#xff0c;用Microsoft Access數據庫文件作為數據提供程序&#xff0c;連接上Crystal Reports的用例的數據庫Xtreme2. 創建新Crystal Reports報表 在工程項目中添加一個…

leetcode 1128. 等價多米諾骨牌對的數量

給你一個由一些多米諾骨牌組成的列表 dominoes。 如果其中某一張多米諾骨牌可以通過旋轉 0 度或 180 度得到另一張多米諾骨牌&#xff0c;我們就認為這兩張牌是等價的。 形式上&#xff0c;dominoes[i] [a, b] 和 dominoes[j] [c, d] 等價的前提是 ac 且 bd&#xff0c;或是…

海量數據尋找最頻繁的數據_尋找數據科學家的“原因”

海量數據尋找最頻繁的數據Start with “Why” - Why do we do the work we do?從“為什么”開始-我們為什么要做我們所做的工作&#xff1f; The question of “Why” is always a big question. Plus, it always makes you look smart in a meeting!“ 為什么 ”的問題始終是…

C語言中局部變量和全局變量 變量的存儲類別

C語言中局部變量和全局變量 變量的存儲類別(static,extern,auto,register) 局部變量和全局變量在討論函數的形參變量時曾經提到&#xff0c;形參變量只在被調用期間才分配內存單元&#xff0c;調用結束立即釋放。這一點表明形參變量只有在函數內才是有效的&#xff0c;離開該函…

營銷 客戶旅程模板_我如何在國外找到開發人員的工作:我從營銷到技術的旅程...

營銷 客戶旅程模板by Dimitri Ivashchuk由Dimitri Ivashchuk 我如何在國外找到開發人員的工作&#xff1a;我從營銷到技術的旅程 (How I got a developer job abroad: my journey from marketing to tech) In this post, I’ll go into the details of how I, a Ukrainian mar…

keepalive的作用

keepalive的作用是實現高可用,通過VIP虛擬IP的漂移實現高可用.在相同集群內發送組播包,master主通過VRRP協議發送組播包,告訴從主的狀態. 一旦主掛了從就選舉新的主,實現高可用 LVS專屬技能,通過配置文件控制lvs集群節點.對后端真實服務器進行健康檢查. 轉載于:https://www.cnb…

scrapy.Spider的屬性和方法

scrapy.Spider的屬性和方法 屬性: name:spider的名稱,要求唯一 allowed_domains:允許的域名,限制爬蟲的范圍 start_urls:初始urls custom_settings:個性化設置,會覆蓋全局的設置 crawler:抓取器,spider將綁定到它上面 custom_settings:配置實例,包含工程中所有的配置變量 logge…

php時間操作函數總結,基于php常用函數總結(數組,字符串,時間,文件操作)

數組:【重點1】implode(分隔,arr) 把數組值數據按指定字符連接起來例如&#xff1a;$arrarray(1,2,3,4);$strimplode(-,$arr);explode([分隔],arr)按指定規則對一個字符串進行分割&#xff0c;返回值為數組 別名joinarray_merge()合并一個或多個數組array_combine(array keys, …

kaggle比賽數據_表格數據二進制分類:來自5個Kaggle比賽的所有技巧和竅門

kaggle比賽數據This article was originally written by Shahul ES and posted on the Neptune blog.本文最初由 Shahul ES 撰寫&#xff0c; 并發布在 Neptune博客上。 In this article, I will discuss some great tips and tricks to improve the performance of your stru…

leetcode 1579. 保證圖可完全遍歷(并查集)

Alice 和 Bob 共有一個無向圖&#xff0c;其中包含 n 個節點和 3 種類型的邊&#xff1a; 類型 1&#xff1a;只能由 Alice 遍歷。 類型 2&#xff1a;只能由 Bob 遍歷。 類型 3&#xff1a;Alice 和 Bob 都可以遍歷。 給你一個數組 edges &#xff0c;其中 edges[i] [typei,…

別把“運氣”當“實力”

成功是兩分靠努力&#xff0c;八分靠天命–何英圻何英圻先生&#xff0c;大家口中的Steven&#xff0c;是臺灣網路創業圈的傳奇人物。他先后創辦力傳(Ubid)與興奇(Monday)兩家公司&#xff0c;最后都以高價出售給北美網路巨人—Ubid在2002年以美金950萬賣給eBay&#xff0c;而M…

品牌推廣前期要進行哪些針對性的步驟?

企業在品牌推廣前需要制訂一系列有針對性和連續性的步驟&#xff0c;這些步驟定睛于長期策略&#xff0c;而且要適應目標客戶的使用方式和習慣。在企業內部導入品牌VI是前提&#xff0c;外部的宣傳則是強調品牌所宣揚的內涵和精神實質&#xff0c;總體來說&#xff0c;這只是一…

php的set 容器,關于STL中set容器的一些總結

1.關于setC STL 之所以得到廣泛的贊譽&#xff0c;也被很多人使用&#xff0c;不只是提供了像vector, string, list等方便的容器&#xff0c;更重要的是STL封裝了許多復雜的數據結構算法和大量常用數據結構操作。vector封裝數組&#xff0c;list封裝了鏈表&#xff0c;map和set…

強化學習應用于組合優化問題_如何將強化學習應用于現實生活中的計劃問題

強化學習應用于組合優化問題by Sterling Osborne, PhD Researcher作者&#xff1a;斯特林奧斯本(Sterling Osborne)&#xff0c;博士研究員 如何將強化學習應用于現實生活中的計劃問題 (How to apply Reinforcement Learning to real life planning problems) Recently, I hav…

導入導出報錯

導入導出報錯&#xff1a;另&#xff1a;右鍵--共享&#xff1a;停止共享&#xff1b;可能無效。此時&#xff0c;可以通過修改文件夾的權限&#xff0c;來達到停止共享的目的&#xff1b;轉載于:https://www.cnblogs.com/chenjx/p/7107336.html

leetcode 724. 尋找數組的中心索引

給定一個整數類型的數組 nums&#xff0c;請編寫一個能夠返回數組 “中心索引” 的方法。 我們是這樣定義數組 中心索引 的&#xff1a;數組中心索引的左側所有元素相加的和等于右側所有元素相加的和。 如果數組不存在中心索引&#xff0c;那么我們應該返回 -1。如果數組有多…

基于mosquitto的MQTT服務器---SSL/TLS 單向認證+雙向認證

配置單/雙向認證 1.生成證書 使用如下shell 來生成證書&#xff1a; # * Redistributions in binary form must reproduce the above copyright# notice, this list of conditions and the following disclaimer in the# documentation and/or other materials provided wi…