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

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

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

問(wèn)題描述

我目前正在進(jìn)行代碼審查,下面的代碼讓我大吃一驚.我看到此代碼存在多個(gè)問(wèn)題.你是否同意我的觀點(diǎn)?如果是這樣,我該如何向我的同事解釋這是錯(cuò)誤的(固執(zhí)的類(lèi)型......)?

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...)?

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

代碼:

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;
    }
}

推薦答案

口頭禪是:

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

因此:

  • 你不應(yīng)該抓住一般例外.

在您的情況下,是的,您應(yīng)該只捕獲這些異常并做一些有用的事情(可能不僅僅是吃掉它們——您可以在記錄它們之后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.

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

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
}

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

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.

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

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

相關(guān)文檔推薦

Is there a way to know if someone has bookmarked your website?(有沒(méi)有辦法知道是否有人為您的網(wǎng)站添加了書(shū)簽?)
Use of Different .Net Languages?(使用不同的 .Net 語(yǔ)言?)
Is there a C# library that will perform the Excel NORMINV function?(是否有執(zhí)行 Excel NORMINV 函數(shù)的 C# 庫(kù)?)
Determining an #39;active#39; user count of an ASP.NET site(確定 ASP.NET 站點(diǎn)的“活動(dòng)用戶(hù)數(shù))
Select x random elements from a weighted list in C# (without replacement)(從 C# 中的加權(quán)列表中選擇 x 個(gè)隨機(jī)元素(無(wú)需替換))
Best way to keep track of current online users(跟蹤當(dāng)前在線(xiàn)用戶(hù)的最佳方式)
主站蜘蛛池模板: 亚洲精选久久 | 久久久国产精品视频 | 999精品在线观看 | 国产成人免费视频网站视频社区 | 久久人爽| 九色 在线 | 在线观看国产视频 | 精品国产欧美在线 | 国产精品视频一区二区三 | 亚洲二区精品 | 国产精品高清在线 | 四虎永久免费影院 | 精品无码久久久久久国产 | 国产精品免费一区二区三区四区 | 久久久久久久久久久久91 | 三级黄色片在线 | 欧美成人一区二区三区片免费 | 草久久久| 国产精品视频网 | 午夜国产一级片 | 国产综合久久 | 国产视频福利在线观看 | 91精品在线播放 | 久久免费高清 | 成人在线视频一区二区三区 | 亚洲精品一区二区 | 精品在线一区 | 午夜精品一区二区三区三上悠亚 | 成人在线观看免费 | 欧美日韩中 | 亚洲精品久久久久国产 | 亚洲国产欧美一区二区三区久久 | 国产福利在线 | 亚洲精品久久久蜜桃 | 国产视频第一页 | 情侣黄网站免费看 | 亚洲第一在线视频 | 91精品国产一区二区三区蜜臀 | 五月天国产视频 | 女女爱爱视频 | 国产精品欧美一区二区 |