【发布时间】:2015-06-26 06:02:11
【问题描述】:
我正在编写一个 C# 应用程序(在 Linux 中使用单声道但没关系),并且我正在使用 duplicati dll 进行编程。我希望程序永远不会崩溃,所以我试图捕获每个异常。现在的问题是抛出了异常,我无法捕获它。也许来自一个线程?!?
旁注:仅出于测试目的,我故意尝试备份到我没有权限的位置。如果我给予许可,则不会出错。
代码如下:
try {
Interface i = new Interface(backend, options);
result = i.Backup(folders.ToArray());
} catch (Exception e) {
//Write to log.
//Here is no throw; !!
}
我得到以下堆栈跟踪:
Error : System.Exception: Failed to retrieve file listing: Access to the path "/home/pi/test" is denied. ---> System.UnauthorizedAccessException: Access to the path "/home/pi/test" is denied.
at System.IO.Directory.GetFileSystemEntries (System.String path, System.String searchPattern, FileAttributes mask, FileAttributes attrs) [0x00000] in <filename unknown>:0
at System.IO.Directory.GetFiles (System.String path, System.String searchPattern) [0x00000] in <filename unknown>:0
at System.IO.Directory.GetFiles (System.String path) [0x00000] in <filename unknown>:0
at Duplicati.Library.Backend.File.List () [0x00000] in <filename unknown>:0
at Duplicati.Library.Main.BackendWrapper.ListInternal () [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at Duplicati.Library.Main.BackendWrapper.ListInternal () [0x00000] in <filename unknown>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
为什么我无法捕获所有异常?我是不是做错了什么?
【问题讨论】:
-
不直接相关:一些异常比其他异常更异常 - 比如 StackOverflow... 回到问题 - 堆栈与显示的代码并不真正对应 - 很难提出任何建议,但确实抛出了异常其他线程上的代码不会被该代码捕获。
-
报错是哪一行?
-
并非所有异常都是可恢复的。假设发生此异常,它会中断您的代码并转到处理程序。你的程序应该在哪里恢复?
-
@AlexeiLenenkov:好的,谢谢,我明白了!但是在这里它不能是堆栈,因为当我设置正确的权限时我的程序可以工作。那么有没有可能阻止这样的程序崩溃?!?
-
@FahadJameel:这就是问题所在,我看不到它,因为堆栈跟踪中没有任何内容,但这些行是对库的唯一调用,所以它必须在那里
标签: c# exception exception-handling mono try-catch