C# - finally子句中的异常处理

Ler*_*ica 1 c# exception-handling

标题有点误导,但这个问题对我来说似乎非常简单.我有try-catch-finally阻止.我只想在finally块中抛出异常时才在块中执行代码try.现在代码的结构是:

try
{
  //Do some stuff
}
catch (Exception ex)
{
  //Handle the exception
}
finally
{
  //execute the code only if exception was thrown.
}
Run Code Online (Sandbox Code Playgroud)

现在我能想到的唯一解决方案就是设置一个标志:

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.
  }
}
Run Code Online (Sandbox Code Playgroud)

并不是说我看到了一些不好的东西,但想知道是否有另一种(更好的)方法来检查是否存在抛出的异常?

Yan*_*eau 12

怎么样的:

try
{
    // Do some stuff
}
catch (Exception ex)
{
    // Handle the exception
    // Execute the code only if exception was thrown.
}
finally
{
    // This code will always be executed
}
Run Code Online (Sandbox Code Playgroud)

那是什么Catch块!