久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

捕獲非特定異常(例如 System.Exception)是一種不好的

Is this a bad practice to catch a non-specific exception such as System.Exception? Why?(捕獲非特定異常(例如 System.Exception)是一種不好的做法嗎?為什么?)
本文介紹了捕獲非特定異常(例如 System.Exception)是一種不好的做法嗎?為什么?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我目前正在進行代碼審查,下面的代碼讓我大吃一驚.我看到此代碼存在多個問題.你是否同意我的觀點?如果是這樣,我該如何向我的同事解釋這是錯誤的(固執的類型......)?

I am currently doing a code review and the following code made me jump. I see multiple issues with this code. Do you agree with me? If so, how do I explain to my colleague that this is wrong (stubborn type...)?

  • 捕獲一個通用異常(Exception ex)
  • 使用if (ex is something)"代替另一個 catch 塊
  • 我們吃SoapException、HttpException 和WebException.但是,如果 Web 服務失敗了,就沒有什么可做的了.

代碼:

try
{
    // Call to a WebService
}
catch (Exception ex)
{
    if (ex is SoapException || ex is HttpException || ex is WebException)
    {
        // Log Error and eat it.
    }
    else
    {
        throw;
    }
}

推薦答案

口頭禪是:

  • 您應該只在以下情況下捕獲異常你可以妥善處理它們

因此:

  • 你不應該抓住一般例外.

在您的情況下,是的,您應該只捕獲這些異常并做一些有用的事情(可能不僅僅是吃掉它們——您可以在記錄它們之后throw).

In your case, yes, you should just catch those exceptions and do something helpful (probably not just eat them--you could throw after you log them).

您的編碼器正在使用 throw(不是 throw ex),這是 .

Your coder is using throw (not throw ex) which is good.

這是您可以捕獲多個特定異常的方法:

This is how you can catch multiple, specific exceptions:

try
{
    // Call to a WebService
}
catch (SoapException ex)
{
    // Log Error and eat it
}
catch (HttpException ex)
{
    // Log Error and eat it
}
catch (WebException ex)
{
    // Log Error and eat it
}

這幾乎等同于您的代碼所做的.您的開發人員可能這樣做是為了避免重復記錄錯誤并吃掉它"塊.

This is pretty much equivalent to what your code does. Your dev probably did it that way to avoid duplicating the "log error and eat it" blocks.

這篇關于捕獲非特定異常(例如 System.Exception)是一種不好的做法嗎?為什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Is there a way to know if someone has bookmarked your website?(有沒有辦法知道是否有人為您的網站添加了書簽?)
Use of Different .Net Languages?(使用不同的 .Net 語言?)
Is there a C# library that will perform the Excel NORMINV function?(是否有執行 Excel NORMINV 函數的 C# 庫?)
Determining an #39;active#39; user count of an ASP.NET site(確定 ASP.NET 站點的“活動用戶數)
Select x random elements from a weighted list in C# (without replacement)(從 C# 中的加權列表中選擇 x 個隨機元素(無需替換))
Best way to keep track of current online users(跟蹤當前在線用戶的最佳方式)
主站蜘蛛池模板: 手机看片国产 | 国产黄色精品 | 欧美国产日韩视频 | 91蜜桃婷婷狠狠久久综合9色 | 精品一区二区三区在线观看 | 欧美中文字幕 | 久久亚洲成人 | 五月婷婷综合激情 | 国产91热爆ts人妖系列 | 日韩视频在线观看 | 成人免费视频观看视频 | 欧美日韩免费一区二区三区 | 国内自拍偷拍 | 麻豆做爰免费观看 | 色综合视频在线观看 | www.一区| 成人国产精品 | 欧美成人久久 | 国产日韩精品在线 | 亚洲美女一区 | 久久在线免费观看 | 中文字幕有码在线 | 男人影院在线观看 | 91成人在线 | 成人免费小视频 | 日韩欧美精品在线 | 成人黄色大片 | 免费一级片 | 人人爱人人草 | 欧美一级做性受免费大片免费 | 日韩中文一区 | 波多野吉衣一二三区乱码 | 午夜国产在线观看 | 国产精品海角社区 | 国产午夜av | 黄色成人小视频 | 夜夜夜夜操 | 亚洲一区二区三区在线播放 | 久久伊人国产 | 少妇一级淫片免费观看 | 国产激情视频在线观看 |