在使用 .NET 反编译器查看一个没有源代码的应用程序的代码时,我发现了这个:
if (DeleteDisks)
{
using (List<XenRef<VDI>>.Enumerator enumerator3 = list.GetEnumerator())
{
MethodInvoker invoker2 = null;
XenRef<VDI> vdiRef;
while (enumerator3.MoveNext())
{
vdiRef = enumerator3.Current;
if (invoker2 == null)
{
//
// Why do this?
//
invoker2 = delegate {
VDI.destroy(session, vdiRef.opaque_ref);
};
}
bestEffort(ref caught, invoker2);
}
}
}
if (caught != null)
{
throw caught;
}
private static void bestEffort(ref Exception caught, MethodInvoker func)
{
try
{
func();
}
catch (Exception exception)
{
log.Error(exception, exception);
if (caught == null)
{
caught = exception;
}
}
}
为什么不能直接调用
VDI.destroy()
?这只是一种包装相同模式的方式 try { do something } catch { log error }
,如果经常使用它吗?