C# 6.0:Exception Filter——帶條件的異常處理

來源:互聯網
上載者:User

標籤:style   blog   http   ar   io   color   os   使用   sp   

C#6.0 對異常處理有兩處改進,一個是在上一篇文章中我們討論了的在catch和finally中使用await,另一個是exception filter。在catch和finally中使用await是一個開發人員歡迎的功能,無疑exception filter是另一個給開發人員處理異常帶來極大便利的新功能。那麼神秘是exception filter那?它是一個通過返回true或false到catch塊中的條件陳述式協助我們過濾篩選exception的功能。

下面的代碼塊是一個exception filter的簡單說樣本。

try{    // Some exception}catch (Exception e) if (e.InnerException != null){    // handle the exception}finally{}
View Code

如上面的代碼所示,一個if語句添加到了exception的後面。它表示catch塊中的代碼只在這個if條件為true的時候執行。我們使用不同的條件來定義多個catch塊。隨著一級級執行,它們也會一個個的被調用。

try{    // Some exception}catch (InvalidOperationException ioe) if (Condition1){    // handle the exception}catch (FieldAccessException fae) if (Condition2){    // handle the exception}catch(Exception exp){}finally{}
View Code

 

C# 6.0:Exception Filter——帶條件的異常處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.