On 02/12/2010 18:26, David Emerson wrote:
Wouldn't it be nice if we had a try..except..finally statement
supported in FPC. All-in-one.
We do: it is called "try try"

try try
   // ...
except
   // ...
finally
   // ...

I wonder where the *big* advantage of this "try..except..finally" is?

Looking at your code above there are 2 (simplified) cases.
1) you handle the exception in the except case, and it is ot re-risen
2) you re-raise it again
Variations of the 2 above:
3) a combination: sometimes handle,sometimes raise
4) the need to cover a new exception that may arise while handling the first exception

Lets look at them:
1)
  try
    DoDangerousStuff;
  except
    FixTheDamege
  end;
  DoYourFinalizationStuff;

Since any exception is handled, and no new exception is risen in the except block, the code after the except block is *always* executed. It does not need a "finally" statement.


2) re-raise the exception
  try
    DoDangerousStuff;
  except
    DoYourFinalizationStuff;
    LogTheExceptionAnd_Raise_ItAgain
  end;
  DoYourFinalizationStuff;

In case of an exception, the 2nd "DoYourFinalizationStuff;" is not called. To avoid code duplication, you would have to put the code into a local subroutine.

But yes, in this ase, an extra finally may be slighly more readable


3 and 4 are handled in the same way as 2.

Additionally, if your finalization code is only "FreeAndNil(SomeVar)", then you can add it in and after the exception block, and even allow it to run twice. The 2nd run does no damage.

my 2 cents
Martin

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to