这是对 “throw”和“throw ex”有区别吗 的跟进问题。
是否有一种方法可以提取一个新的错误处理方法而不重置堆栈跟踪?
[编辑] 我将尝试使用“内部方法”和Earwicker提供的另一个答案,看看哪个可以更好地标记一个答案。
这是对 “throw”和“throw ex”有区别吗 的跟进问题。
是否有一种方法可以提取一个新的错误处理方法而不重置堆栈跟踪?
[编辑] 我将尝试使用“内部方法”和Earwicker提供的另一个答案,看看哪个可以更好地标记一个答案。
在.NET Framework 4.5中,现在有一个ExceptionDispatchInfo,支持这种情况。它允许捕获完整的异常并从其他地方重新抛出它,而不会覆盖包含的堆栈跟踪。
由于评论要求提供代码示例
using System.Runtime.ExceptionServices;
class Test
{
private ExceptionDispatchInfo _exInfo;
public void DeleteNoThrow(string path)
{
try { File.Delete(path); }
catch(IOException ex)
{
// Capture exception (including stack trace) for later rethrow.
_exInfo = ExceptionDispatchInfo.Capture(ex);
}
}
public Exception GetFailure()
{
// You can access the captured exception without rethrowing.
return _exInfo != null ? _exInfo.SourceException : null;
}
public void ThrowIfFailed()
{
// This will rethrow the exception including the stack trace of the
// original DeleteNoThrow call.
_exInfo.Throw();
// Contrast with 'throw GetFailure()' which rethrows the exception but
// overwrites the stack trace to the current caller of ThrowIfFailed.
}
}
是的,这就是 InnerException 属性的作用。
catch(Exception ex)
{
throw new YourExceptionClass("message", ex);
}
这将允许您添加自己的逻辑,然后抛出自己的异常类。YourExceptionClass实例的StackTrace将来自于此代码块内部,但InnerException将是您捕获的异常,带有它之前所拥有的StackTrace。
不确定你是否指的是这个,但是我在你的另一个问题中提供了建议来解决这个问题。
如果你的处理程序返回一个布尔值表示异常是否被处理,你可以在catch语句中使用它:
catch (Exception ex) {
if (!HandleException(ex)) {
throw;
}
}
您不希望使用原始堆栈跟踪创建新的异常。这是误导性的,因为该堆栈跟踪并没有创建新的异常。
但是,您可以将原始异常作为“InnerException”放入新的异常中。这样做是否符合您的要求?
在其他方式中,过滤异常应该是可行的,但在C#中不行。然而,在VB.NET中是可行的,并且BCL本身利用这一点,通过使用少量VB.NET代码以更方便的方式过滤异常。
fileToDeleteLater
文件。托管语言极大地减少了程序状态中的自由度,但它们并不能神奇地将其减少到只有可取状态是可访问的。 - Daniel Earwicker