久久久久久久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(跟蹤當前在線用戶的最佳方式)
主站蜘蛛池模板: 国产女人与拘做视频免费 | 天天射网站 | 午夜免费视频 | 日韩高清www| 欧美成人一区二区三区 | 视频一区在线播放 | 天天操天天插天天干 | 日韩欧美三区 | 国产精品久久久久久久岛一牛影视 | 免费一区二区在线观看 | 一区二区视屏 | 欧美精品一区二区三区在线播放 | 高清视频一区二区三区 | 一区二视频| 久久精品久久久久久 | 成年人视频免费在线观看 | 久久久国产一区二区三区四区小说 | 日韩不卡一区二区三区 | 日韩伦理电影免费在线观看 | 中文字幕一区二区三区四区 | 久草中文在线 | 国产一区二区在线免费观看 | 久久性 | 天天草夜夜骑 | 超碰在线国产 | 亚洲精品一区二区三区蜜桃久 | 久久一区视频 | 亚洲精品成人av久久 | 亚洲一区中文字幕在线观看 | 欧美久久一级 | 成人av资源在线 | 伊人伊人 | 日本超碰 | 欧美午夜视频 | 久久久久久www | 国产精品久久久久久久久 | 91久久国产综合久久 | 亚洲精品视 | 欧美日韩免费一区二区三区 | 桃色五月 | 久久久免费 |