Re: Another shitty example of exceptions

From: Bruce Roberts (ber_at_bounceitattcanada.xnet)
Date: 04/06/04


Date: Tue, 6 Apr 2004 14:02:56 -0400


"Skybuck Flying" <nospam@hotmail.com> wrote in message
news:c4tr9t$c6m$1@news2.tilbu1.nb.home.nl...

> Ok, so what does your code like ?
>
> I am thinking it might look something like this:
>
> try
> instruction A;
> instruction B;
> instruction C;
> instruction D;
> except
>
> end;

If it were my code it would look like

    instruction A;
    instruction B;
    instruction C;
    instruction D;

> How do you know what instruction failed ?

I don't care unless its an error I can anticipate and, more importantly,
recover from without affecting a higher level of code.

> How do you know if A or maybe B was created ?

You don't care because the thing won't work without A and B.

> I am wondering about how you clean up after yourself if you don't know
what
> went wrong.

Because one uses try finally to handle clean up, not try except. Try Finally
is designed specifically to handle cleanup. Try except is designed
specifically to handle failure. It is common to find try finally along with
try except, but the reverse is most definitely no true.

>
> Maybe this would work ?
>
> try
> A := Ta.Create;
> B := Tb.Create;
> C := Tc.Create;
> D := Td.Create;
>
> except
>
> finally
> D.Free;
> C.Free;
> B.Free;
> A.Free;
> end;

Sure won't work in Delphi. Won't even compile. But

A := nil;
B := nil;
C := nil;
D := nil;
// . . .
try
    A := tb.Create;
    B := tb.Create;
    C := tb.Create;
    D := tb.Create;
    // . . .
finally
    A.Free;
    B.Free;
    C.Free;
    D.Free;
    end;

does the trick;

>
> So suppose C fails... D will never be created.
>
> Will D.Free make it crash ?

Not if its been properly initialized.

> Or is free safe to use ?

And the Help says?