Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C语言中开发库时,如何进行适当的异常处理?_C#_.net_Dll_Exception Handling - Fatal编程技术网

C# 在C语言中开发库时,如何进行适当的异常处理?

C# 在C语言中开发库时,如何进行适当的异常处理?,c#,.net,dll,exception-handling,C#,.net,Dll,Exception Handling,假设我想创建一个DLL库,以一种易于使用的方式提供公共文件系统服务。该库可用于第三方.NET应用程序。 假设此库仅向公众提供此接口: 公共接口IFileSystems服务 { 无效CreateDirectorystring路径; void DeleteDirectorystring路径; bool DirectoryExistsstring路径; IEnumerable枚举目录字符串路径; 无效删除文件字符串路径; bool文件存在字符串路径; void CopyFilestring sourc

假设我想创建一个DLL库,以一种易于使用的方式提供公共文件系统服务。该库可用于第三方.NET应用程序。 假设此库仅向公众提供此接口:

公共接口IFileSystems服务 { 无效CreateDirectorystring路径; void DeleteDirectorystring路径; bool DirectoryExistsstring路径; IEnumerable枚举目录字符串路径; 无效删除文件字符串路径; bool文件存在字符串路径; void CopyFilestring sourcePath,string destinationPath,bool overwriteExisting; //…和其他方法 } 当使用名称空间中的标准.NET类(如等)实现此接口时,异常处理的最佳实践是什么。。。?
如何使该库健壮并易于客户端代码使用?我如何才能做到这一点,以使用于错误处理的客户端代码干净且不过于复杂?

最佳做法是尽可能从异常中恢复,但如果您不能做任何事情,则让异常传播到调用方并让他们处理它

至于如何使库健壮且易于使用,请尽可能多地为库编写文档。您可以使用XML文档将引发的异常包含在DLL的元数据中

从Directory.CreateDirectorystring获取的注释

通过使用System.IO命名空间中的标准.NET类(如FileInfo、DirectoryInfo、Directory、File等)实现此接口时,异常处理的最佳实践是什么

就我而言,最好的做法是让异常冒出来。作为框架实现者,您不知道API用户的异常处理意图是什么。因为您不知道该用户的意图,所以最好让异常被抛出

通过让异常冒泡,您可以获得异常实际发生位置的完整堆栈跟踪 您还允许基于一个上下文处理想要捕获的异常 根据an的博客帖子,我再也找不到了,我记得他说过最好看到这样的代码:

if (condition) throw new Exception("message");
而不是看到:

try { something(); }
catch (Exception ex) { throw new Exception(); }
因为每次重试异常时都要重置堆栈跟踪。否则,您可以:

try { something(); }
catch (Exception ex) { 
    handleTheExceptionInternallyForYourPurpose();
    throw ex; // simply let the exception flow go its way to the top, to the caller.
}
另外,这完全取决于你的行为期望。假设您不希望允许null值作为path参数,那么在这种情况下,您可能希望抛出ArgumentNullException

public void CreateDirectory(string path) {
    if (path == null) throw new ArgumentNullException("path");
    if (string.IsNullOrWhiteSpace(path)) throw new ArgumentException("path");

    // create your directory here based on the path provided...
}
如何使该库健壮并易于客户端代码使用

我打赌你不能为其他程序员编程。您可能拥有世界上最好的库,一个经验较少或不尊重规则的库可能会以混乱的代码结束。每个程序员都应该遵循一些指导原则,尊重最佳实践和原则。可悲的是,这些原则很少得到遵守,至少在我的世界里是这样。无论你做什么,几乎总会有更好的方式来做事情,这取决于你的环境

我如何做到这一点,以使用于错误处理的客户端代码干净且不过于复杂

复杂的东西对代码来说总是复杂的,所以没有办法逃避。通过坚持原则,使用明天的最佳实践,您将拥有一个简单易懂的库


当涉及到库时,也是一个很好的方法,这样IntelliSense可以在键入时显示它。

什么是清晰的而不是复杂的错误处理?@reniuz:我希望评论。。。真的不确定,我不能给出确切的定义,我希望社区对此有一些共识。对我来说,一件事就是用少量的try/catch块编写客户机代码。。。也许试着用不超过一个或两个挡块的挡块。。。太多的try/catch会使代码混乱。当我做CopyFile时,我只希望抛出一种类型的异常,并在InnerException中提供更多详细信息。不确定这是否是正确的方法,但这就是它目前对我的意义。
public void CreateDirectory(string path) {
    if (path == null) throw new ArgumentNullException("path");
    if (string.IsNullOrWhiteSpace(path)) throw new ArgumentException("path");

    // create your directory here based on the path provided...
}