Delphi Exception Handling - How to clean up properly?

前端 未结 4 1706
生来不讨喜
生来不讨喜 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:43

    At first your code looks a little bit strange. I miss the creation of X.

    X := CreateAnX
    try
      DoSomeThing(X);
    finally
      FreeAndNil(x);
    end;
    

    It is important. Because if you have code like this

    // don't do something like this
    try
      X := CreateAnX
      DoSomeThing(X);
    finally
      FreeAndNil(x);
    end;
    

    you can be lucky and it works. But if the construction fails you can be "lucky" and get an access violation or you have bad luck and get an access violation some times later at an completely different code position.

    An alternative could be

    X := nil;
    try
      X := CreateAnX
      DoSomeThing(X);
    finally
      FreeAndNil(x);
    end;
    

    Where to use except depends on what is your intention. When you want to catch every exception and know all calling code clean its problems (using try finally) then an outer except-block is the way to go

    try
      X := CreateAnX
      try
        DoSomeThing(X);
      finally
        FreeAndNil(x);
      end;
    except
      on e: Exception do
        LogException(e)
    end;      
    

    But always think about it when you want catch all errors. As an example (and I often see it wrong) don't do it in an Indy OnExecute-handler this way. There you must use something like this

    try
      X := CreateAnX
      try
        DoSomeThing(X);
      finally
        FreeAndNil(x);
      end;
    except
      on EIdException do
        raise;
      on e: Exception do
        LogException(e)
    end;      
    

    If you expect an exception because you throw it or (as an example) a conversation can fail, look for the most inner position to catch the error:

    X := CreateAnX
    try
      DoSomeThing(X);
      try
        i := StrToInt(X.Text);
      except
        on EConvertError do
          i := 0;
      end;       
    finally
      FreeAndNil(x);
    end;
    

    don't do it this way

    X := CreateAnX
    try
      try
        DoSomeThing(X);
        i := StrToInt(X.Text);
      except
        on EConvertError do
          i := 0;
      end;       
    finally
      FreeAndNil(x);
    end;
    

    Do this only if you expect an EConvertError in DoSomeThing too. If DoSomeThing throws an EConvertError and you don't expect it, your code has a serious problem which need to be corrected. In this situation ensure that the user can save his work (perhaps as a copy, because his work could be damaged) and ensure you get the info about the problem.

    At least try except is for handling Exceptions not for hiding them.

提交回复
热议问题