捕捉特定主场迎战C#泛型例外 [英] Catching specific vs. generic exceptions in c#

查看:201
本文介绍了捕捉特定主场迎战C#泛型例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题来自对我创建了一个对象,运行code分析。分析说,我应该捕获更具体的异常类型的不仅仅是基本的异常。

This question comes from a code analysis run against an object I've created. The analysis says that I should catch a more specific exception type than just the basic Exception.

你只使用捕捉通用的异常,或试图捉住一个特定的异常,并使用多个catch块默认为一个通用的异常发现自己?

Do you find yourself using just catching the generic Exception or attempting to catch a specific Exception and defaulting to a generic Exception using multiple catch blocks?

一个有问题的code块是如下:

One of the code chunks in question is below:

internal static bool ClearFlags(string connectionString, Guid ID)
{
    bool returnValue = false;
    SqlConnection dbEngine = new SqlConnection(connectionString);
    SqlCommand dbCmd = new SqlCommand("ClearFlags", dbEngine);
    SqlDataAdapter dataAdapter = new SqlDataAdapter(dbCmd);

    dbCmd.CommandType = CommandType.StoredProcedure;
    try
    {
        dbCmd.Parameters.AddWithValue("@ID", ID.ToString());

        dbEngine.Open();
        dbCmd.ExecuteNonQuery();
        dbEngine.Close();

        returnValue = true;
    }
    catch (Exception ex)
    { ErrorHandler(ex); }

    return returnValue;
}

感谢您的咨询。

编辑:下面是从code分析警告

Here is the warning from the code analysis

警告351 CA1031:Microsoft.Design:修改ClearFlags(字符串,GUID)来稳住更具体的例外,不是'异常'或重新抛出异常

Warning 351 CA1031 : Microsoft.Design : Modify 'ClearFlags(string, Guid)' to catch a more specific exception than 'Exception' or rethrow the exception

推荐答案

您应该几乎从来没有赶上顶级异常。

You should almost never catch the top level Exception.

在大多数情况下,你应该捕获并处理最具体的异常可能且仅当有是非常有用的,你可以用它做什么。

In most cases you should catch and handle the most specific exception possible and only if there is something useful you can do with it.

除外(哈哈)这个是,如果你正在追赶用于记录和重新抛出异常,那么它有时是确定抓顶层异常,登录并重新抛出。

The exception (haha) to this is if you are catching for logging and re-throw the exception, then it is sometimes OK to catch a top level Exception, log it and rethrow it.

您应该几乎从来不乘顶级异常,把它吞。这是因为,如果你正赶上顶层异常,你真的不知道你是什么样的处理;是绝对什么事情都可能造成,所以你几乎肯定不能做任何事情,正确处理每一个失败案例。有可能有一些失败,你可能只是想默默地处理和吞咽,但只是吞咽顶层例外也将被吞噬,真正应该被向上抛出你的code办理上涨了一大堆。在您的code例如,你可能想要做的是处理的SQLException和日志+吞下;然后换一个例外,日志和重新抛出。这包括你自己。你还在日志记录了所有的异常,但你只能吞下相当predictable的SQLException这表明你的SQL /数据库问题。

You should almost never catch a top level Exception and swallow it. This is because if you are catching a top level exception you don't really know what you are handling; absolutly anything could have caused it so you will almost certainly not be able to do anything that will handle every single failure case correctly. There probably are some failures that you may just want to silently handle and swallow, but by just swallowing top level Exceptions you'll also be swallowing a whole bunch that really should have been thrown upwards for your code to handle higher up. In your code example what you probably want to do is handle a SQLException and log+swallow that; and then for an Exception, log and rethrow it. This covers yourself. You're still logging all exception types, but your only swallowing the fairly predictable SQLException which indicates problems with your SQL/database.

一个常见的​​做法是只处理每一个你实际上可以解决在这一点上,如果你不能在code这一点上解决它,那么你允许它泡向上例外。如果你不能在一个新的水平了解决它,让它继续向上。如果到达顶部未处理然后显示一个礼貌的道歉与用户(也许试图快速自动保存),并关闭应用程序。它通常被认为是雪上加霜,让应用程序继续未处理的异常运行后,因为你不能predict应用为一些特殊的状况已发生。这只是为了更好地关闭并重新启动该应用获取回期望的状态。

A common practise is to only every handle exceptions that you can actually resolve at that point, if you can't resolve it at that point in code then you allow it to bubble upwards. If you can't resolve it at the next level up, allow it to continue up. If it reaches the top unhandled then display a polite appology to the user (perhaps attempt a quick autosave) and close the app. It's generally considered worse to allow an app to continue running after an unhandled exception because you can't predict the state of the application as something exceptional has occured. It's better just to shutdown and restart the app to get back to an expected state.

这篇关于捕捉特定主场迎战C#泛型例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆