C#无法捕获所有异常
我正在编写一个C#应用程序(在Linux中使用mono,但这不重要),我正在使用duplicati DLL编程。我希望程序永远不会崩溃,所以我尝试捕捉每个异常。现在的问题是抛出了一个异常,而我无法捕获它。也许是一根线 旁注:出于测试目的,我故意尝试备份到一个我没有权限的位置。如果我允许,那么我就不会出错 代码如下所示:C#无法捕获所有异常,c#,exception,exception-handling,mono,try-catch,C#,Exception,Exception Handling,Mono,Try Catch,我正在编写一个C#应用程序(在Linux中使用mono,但这不重要),我正在使用duplicati DLL编程。我希望程序永远不会崩溃,所以我尝试捕捉每个异常。现在的问题是抛出了一个异常,而我无法捕获它。也许是一根线 旁注:出于测试目的,我故意尝试备份到一个我没有权限的位置。如果我允许,那么我就不会出错 代码如下所示: try { Interface i = new Interface(backend, options); result = i.Backup(folders.To
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
错误:系统。异常:检索文件列表失败:对路径“/home/pi/test”的访问被拒绝。-->System.UnauthorizedAccessException:拒绝访问路径“/home/pi/test”。
在System.IO.Directory.GetFileSystemEntries(System.String路径、System.String搜索模式、FileAttributes掩码、FileAttributes属性)[0x00000]中:0
在:0中的System.IO.Directory.GetFiles(System.String路径,System.String搜索模式)[0x00000]处
位于:0中的System.IO.Directory.GetFiles(System.String路径)[0x00000]处
位于:0中的replici.Library.Backend.File.List()[0x00000]
位于:0中的replici.Library.Main.BackendWrapper.ListInternal()[0x00000]处
---内部异常堆栈跟踪的结束---
位于:0中的replici.Library.Main.BackendWrapper.ListInternal()[0x00000]处
at(包装器管理为本机)System.Reflection.monmethod:InternalInvoke(System.Reflection.monmethod,object,object[],System.Exception&)
在System.Reflection.MonMethod.Invoke(System.Object obj、BindingFlags invokeAttr、System.Reflection.Binder Binder、System.Object[]参数、System.Globalization.CultureInfo区域性)[0x00000]中:0
为什么我不能捕获所有异常?我做错什么了吗?捕获块可能正在抛出异常 总之,@gunr217想法的一个变体-创建一个单独的Vis。只有一个文件(类)的Studio项目,它所做的只是调用包装在
try/catch
中的主程序;这是应用程序的最顶层
如果你的库是线程化的,它应该公开一个测试线程异常的东西;很像这个
向AppDomain.UnhandledException事件注册
值得捕获的异常 自由使用
Exception.Data
属性。有时我在类中重写ToString()
,目的是将其放入Exception.Data
在应用程序主体中尽可能精确地捕获(OP似乎已经完成了)。此上下文将帮助您在Exception.Data
中捕获特别相关的信息。阅读图书馆和.NET文档
在应用程序顶部有一个通用的try/catch
避免不添加上下文的重捕获层
重新抛出:throw e
将堆栈跟踪丢弃到该点<代码>抛出
使您的应用程序更加健壮
- 定义默认状态李>
- 强制客户端使用您的构造函数。不要公开属性并期望客户端正确初始化。强调指出,公开属性(状态)不是面向对象编程
- 我讨厌
字符串。下面是我最喜欢的一行实时生产代码:null
if(string.IsNullOrEmpty(myString.Trim())
一些希望有用的链接
-
- Marc Gravell在这里说话
- “GetDirectory()方法的缺陷。”
-
- 马克·格雷威尔回答
- 将程序设置为以管理员身份运行
-
- 具有指向未处理异常内容的进一步链接
- 该
catch
块可能正在抛出异常
总之,@gunr217想法的一个变体-创建一个单独的Vis。只有一个文件(类)的Studio项目,它所做的只是调用包装在try/catch
中的主程序;这是应用程序的最顶层
如果你的库是线程化的,它应该公开一个测试线程异常的东西;很像这个
向AppDomain.UnhandledException事件注册
值得捕获的异常 自由使用
Exception.Data
属性。有时我在类中重写ToString()
,目的是将其放入Exception.Data
在应用程序主体中尽可能精确地捕获(OP似乎已经完成了)。此上下文将帮助您在Exception.Data
中捕获特别相关的信息。阅读图书馆和.NET文档
在应用程序顶部有一个通用的try/catch
避免不添加上下文的重捕获层
重新抛出:throw e
将堆栈跟踪丢弃到该点<代码>抛出
使您的应用程序更加健壮
- 定义默认状态李>
- 强制客户端使用您的构造函数。不要公开属性并期望客户端正确初始化。强调指出,公开属性(状态)不是面向对象编程
- 我讨厌
字符串。下面是我最喜欢的一行实时生产代码:null
if(string.IsNullOrEmpty(myString.Trim())
一些希望有用的链接
-
- Marc Gravell在这里说话
- “GetDirectory()方法的缺陷。”
-
- 马克·格雷威尔回答
- 将程序设置为以管理员身份运行
-
- 有未处理的ex的进一步链接
Error : System.Exception: Failed to retrieve file listing: Access to the path...
mono HelloDup.exe "/tmp" File: Local folder or drive
ls /home/private/privateinfo ls: : Permission denied mono HelloDup.exe "/home/private/privateinfo" Exception: Access to the path "/home/private/privateinfo" is denied.: Type:System.UnauthorizedAccessException
mono HelloDup.exe "/foobar" Exception: The folder /foobar does not exist: Type:Duplicati.Library.Interface.FolderMissingException
ls -l /noperms/private.txt --w------- 1 root wheel 0 Jun 25 14:16 /noperms/private.txt mono HelloDup.exe "/noperms/private.txt" Exception: The folder /noperms/private.txt does not exist: Type:Duplicati.Library.Interface.FolderMissingException
try { var file = new Duplicati.Library.Backend.File(args[0], options); file.CreateFolder(); Console.WriteLine ("File: {0}", file.DisplayName); } catch (Exception e) { Console.WriteLine ("Exception: {0}: Type:{1}", e.Message, e.GetType()); }
- 有未处理的ex的进一步链接