我应该抓住一切可能的特殊例外情况或只是一般的例外,敷在自定义的? [英] Should I catch all possible specific exceptions or just general Exception and wrap it in custom one?

查看:140
本文介绍了我应该抓住一切可能的特殊例外情况或只是一般的例外,敷在自定义的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我想反序列化一些XML文件到一个强类型的对象。在此情况下的XML文件不能被反序列化(无论何种原因),我只是想创建一个默认的对象,并继续正常的程序流程,而不显示任何错误给用户。 (其实这个应用是作为Windows服务运行,所以有没有为它的用户...例如,假设应用程序试图加载配置文件,如果失败,则只需使用默认配置)。

Let's say I want to deserialize some XML file to a strongly typed object. In case this XML file can't be deserialized (for whatever reason) I would just create a default object and continue normal application workflow without showing any errors to the user. (actually this application is running as Windows service so there is no user for it... for example, imagine application trying to load config file and if fails then just use default config).

我的问题是如何创造反序列化()方法,使得它很容易通过调用code使用?我的主要问题是如何处理异常......这是我的研究工作。

My questions is how to create Deserialize() method such that it is easy to use by calling code? My main concerns are how to handle exceptions... Here is my research.

解决方案1:

这是最简单的方法:

    public static T Deserialize<T>(string xml)
    {
        var serializer = new XmlSerializer(typeof (T));
        using (var sr = new StreamReader(xml))
        using (var reader = XmlReader.Create(sr))
            return (T) serializer.Deserialize(reader);
    }

我觉得这种方法的使用将是非常艰苦的调用code,因为我不得不处理,可以通过任何这些方法/构造函数抛出的所有可能的异常。调用code不关心这个,只在乎操作成功与否。

I think usage of this method would be very hard in the calling code because I'd have to handle all possible exceptions that can be thrown by any of these methods/constructors. Calling code doesn't care about that, it only cares whether operation succeeded or not.

因此​​,这里是我的第二次​​尝试:

So here is my second try:

解决方案2:

    public static T Deserialize<T>(string xml)
    {
        try
        {
            var serializer = new XmlSerializer(typeof (T));
            using (var sr = new StreamReader(xml))
            using (var reader = XmlReader.Create(sr))
                return (T) serializer.Deserialize(reader);
        }
        catch (ArgumentException ex)
        {
            throw new XmlDeserializeException(ValidationExceptionMsg, ex);
        }
        catch (IOException ex)
        {
            throw new XmlDeserializeException(ValidationExceptionMsg, ex);
        }
        catch (XmlSchemaException ex)
        {
            throw new XmlDeserializeException(ValidationExceptionMsg, ex);
        }
        catch (InvalidOperationException ex)
        {
            throw new XmlDeserializeException(ValidationExceptionMsg, ex);
        }
    }

在这里,我创建了一个称为XmlDeserializeException自定义异常,并用它来包装,可以通过方法try块抛出(如MSDN中指定)的所有异常。现在,调用code只能赶上XmlDeserializeException知道有一个错误。但是我不知道怎么好,是该解决方案......如果我需要再创造了很多这样的方法他们都将有很多catch块中,只有包装异常自定义异常。

Here I have created a custom exception called XmlDeserializeException and use it to wrap all exceptions that can be thrown by methods (as specified in MSDN) in the try block. Now, the calling code should only catch XmlDeserializeException to know there was an error. However I am not sure how good is this solution... If I need to create a lot of methods like this then all of them would be having a lot of catch blocks that only wrap exception to a custom exception.

所以我想将以下code是更好的:

So I was wondering would following code is better:

解决方案3:

    public static T Deserialize<T>(string xml)
    {
        try
        {
            var serializer = new XmlSerializer(typeof (T));
            using (var sr = new StreamReader(xml))
            using (var reader = XmlReader.Create(sr))
                return (T) serializer.Deserialize(reader);
        }
        catch (Exception ex)
        {
            throw new XmlDeserializeException(ValidationExceptionMsg, ex);
        }
    }

下面我赶上一般例外,敷在定制XmlDeserializeException。这样,我已经减少code编写,没有code冗余和有较少的混乱。调用code将再次只需要赶XmlDeserializeException像在溶液中2。

Here I catch general Exception and wrap it in custom XmlDeserializeException. This way I have reduced code writing, there is no code redundancy and there is less clutter. The calling code will again only have to catch XmlDeserializeException like in solution 2.

哪种解决方案,我应该使用,为什么?有没有什么更好的办法?请记住,我想用这个反序列化()方法的情况下,这不是一个库/框架,而是一个应用程序,它不具有用户交互性。

Which solution should I use and why? Is there any better way? Please keep in mind the scenario where I want to use this Deserialize() method, this is not a library/framework but rather an application that doesn't have user interactivity.

推荐答案

如果您处理异常以同样的方式真的是具有不同的捕捞量,在这种情况下,你把杂乱到最低限度,去与解决方案3没有意义的。

If you handle the exceptions the same way there really is no point in having different catches, in which case you keep clutter to a minimum and go with solution 3.

解决方案2只是一大堆更多code做同样的事情。

Solution 2 is just a whole lot more code to do the exact same thing.

这篇关于我应该抓住一切可能的特殊例外情况或只是一般的例外,敷在自定义的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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