在我的代码中,我遇到了一个情况,即会抛出一个System.Reflection.TargetInvocationException异常。在某些特定情况下,我知道如何处理根异常,但我希望抛出所有其他异常。我可以想到两种方法来实现这一点,但我不确定哪种更好。
1.
1.
try
{
//code
}
catch (System.Reflection.TargetInvocationException ex)
{
if (typeof(ex.InnerException) == typeof(SpecificException))
{
//fix
}
else
{
throw ex.Innerexception;
}
}
2.
try
{
//code
}
catch (System.Reflection.TargetInvocationException ex)
{
try
{
throw ex.InnerException;
}
catch (SpecificException exSpecific)
{
//fix
}
}
我知道通常抛出异常会很慢,所以我认为第一个方法可能更快。或者,有没有我没想到的更好的方法?