拦截里面IDisposable.Dispose异常 [英] Intercepting an exception inside IDisposable.Dispose
问题描述
在 IDisposable.Dispose
方法是有办法弄清楚,如果一个异常被抛出?
In the IDisposable.Dispose
method is there a way to figure out if an exception is being thrown?
using (MyWrapper wrapper = new MyWrapper())
{
throw new Exception("Bad error.");
}
如果抛出一个异常,在使用
发言中,我想知道这件事情的时候的IDisposable
对象设置
If an exception is thrown in the using
statement I want to know about it when the IDisposable
object is disposed.
推荐答案
否,有没有办法做到这一点,在.NET框架中,则无法计算出电流异常,而─是 - 被抛出的最后条款。
No, there is no way to do this in the .Net framework, you cannot figure out the current-exception-which-is-being-thrown in a finally clause.
请参阅此职位上我的博客,与在类似的模式比较红宝石,它突出我觉得差距与IDisposable模式存在。
See this post on my blog, for a comparison with a similar pattern in Ruby, it highlights the gaps I think exist with the IDisposable pattern.
Ayende有一个绝招,让你<一个href="http://ayende.com/Blog/archive/2007/06/20/Did-you-know-Find-out-if-an-exception-was-thrown.aspx">detect异常发生的,但是,它不会告诉你这是其中的例外。
Ayende has a trick that will allow you to detect an exception happened, however, it will not tell you which exception it was.
这篇关于拦截里面IDisposable.Dispose异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!