让我们以更少的开销再试一次:
如何确定异常是否为 CorruptedStateException?
据我所知,所有 CorruptedStateExceptions 都没有继承(独占)的通用超类,而且我发现没有其他属性/标志/属性可以用来识别它们。
现在我能想到的最好的方法是分两步工作:在下面的情况下ThirdPartyCall
,有属性[HandleProcessCorruptedStateExceptions]
并[SecurityCritical]
设置为捕获 CorruptedStateExceptions 而ThirdPartyCallInternal
没有。如果ThirdPartyCallInternal
捕捉到一个异常,它就不是 CSE,如果只ThirdPartyCall
捕捉到它,它就是一个。
[HandleProcessCorruptedStateExceptions]
[SecurityCritical]
public static void ThirdPartyCall()
{
bool thrownExceptionIsCorruptedState = true;
try
{
ThirdPartyCallInternal(ref thrownExceptionIsCorruptedState);
}
catch (Exception ex)
{
if (thrownExceptionIsCorruptedState)
{
//This is pretty much the only thing we'd like to do...
log.Fatal("CorruptedStateException was thrown",ex);
}
throw;
}
}
private static void ThirdPartyCallInternal(ref bool thrownExceptionIsCorruptedState)
{
try
{
ThirdPartyLibrary.DoWork();
}
catch (Exception)
{
//Exception was caught without HandleProcessCorruptedStateExceptions => it's not a corruptedStateException
thrownExceptionIsCorruptedState = false;
throw;
}
}
是否有任何其他(更清洁,更简单,......)方法来确定异常是否是 CorruptedStateException 会导致应用程序关闭?