标题有点误导,但这个问题对我来说似乎很直截了当。我有try-catch-finally
块。finally
仅当从块中引发异常时,我才想执行块中的代码try
。现在的代码结构是:
try
{
//Do some stuff
}
catch (Exception ex)
{
//Handle the exception
}
finally
{
//execute the code only if exception was thrown.
}
现在我能想到的唯一解决方案是设置一个标志,如:
try
{
bool IsExceptionThrown = false;
//Do some stuff
}
catch (Exception ex)
{
IsExceptionThrown = true;
//Handle the exception
}
finally
{
if (IsExceptionThrown == true)
{
//execute the code only if exception was thrown.
}
}
并不是说我在这方面看到了什么不好的东西,而是想知道是否有另一种(更好的)方法来检查是否有抛出的异常?