Determine if executing in finally block due to exception being thrown

前端 未结 8 1268
遥遥无期
遥遥无期 2020-12-16 10:01

Is it possible to determine if code is currently executing in the context of a finally handler as a result of an exception being thrown? I\'m rather fond of usi

8条回答
  •  没有蜡笔的小新
    2020-12-16 10:29

    The best I can come up with would be:

    using (var scope = MyScopedBehavior.Begin())
    {
      try
      {
        //Do stuff with scope here
      }
      catch(Exception)
      {
        scope.Cancel();
        throw;
      }
    }
    

    Of course, scope.Cancel() would make sure nothing happens in Dispose()

提交回复
热议问题