Delphi Exception Handling - How to clean up properly?

前端 未结 4 1702
生来不讨喜
生来不讨喜 2021-01-04 20:22

I\'m looking at some code in an application of ours and came across something a little odd from what I normally do. With exception handling and cleanup, we (as well as many

4条回答
  •  囚心锁ツ
    2021-01-04 20:38

    It all depends if the code in your finally block can raise an exception itself (then it needs to be protected by an upper level try except), or if you need something in your exception handling that should be freed later (then it needs to be freed in an upper level finally block).

    All that means that sometimes you can even have some code like:

      try
        try
          try
            CouldCauseError(X);
          except
            HandleErrorWith(X);
          end;
        finally
          FreeAndNil(X); // and/or any resource cleanup
        end;
      except
        CatchAllError;
      end;
    

提交回复
热议问题