2

I have a method in dll

    private static void SaveXmlStringToXmlFile(string xmlString, string xmlFileFullPath)
    {
        XDocument.Parse(xmlString).Save(xmlFileFullPath);
    }

I know that Parse and Save can throw several exceptions. Anyway I have no idea how to handle these exceptions because it's a dll.

Put a try/catch throw; around the code. Isn't this redundant to the present code?

What's the best practice?

EDIT: Let's assume the user of the dll doen't have any influence on what xmlString could be.

4

1 回答 1

2

我相信在涉及异常时,另一个程序集(例如 DLL)中的代码和您的入口程序集中的代码之间没有特别的区别。

您应该将每个异常的 try/catch 准确地放在有足够信息来处理该异常的位置(并从中恢复,如果这是您想做的事情)。与任何其他异常一样,您应该根据异常的类型和可以引发它的嵌套级别来推断这一点,而不是它源自的程序集。

我通常使用的经验法则是:如果SaveXmlStringToXmlFile失败,它的调用者是否应该像什么都没发生一样继续?如果不是(也就是说,如果调用者中的以下语句应该被放弃),那么不要将 try/catch 放入SaveXmlStringToXmlFile,而是更高。

于 2013-05-28T07:04:39.360 回答