Exception Safe Programming

Jarrett Billingsley kb3ctd2 at yahoo.com
Sat Feb 24 15:21:39 PST 2007


"Saaa" <empty at needmail.com> wrote in message 
news:erqh38$2hen$1 at digitalmars.com...
> On the website there is the following example:
>
> Transaction abc()
> {
>    Foo f;
>    Bar b;
>
>    f = dofoo();
>    try
>    {
>    b = dobar();
>    return Transaction(f, b);
>    }
>    catch (Object o)
>    {
>    dofoo_undo(f);
>    throw o;
>    }
> }
>
> When f=dofoo() is run and doesn't succeed I suspect that f hasn't changed 
> and dofoo has thrown an exception.
> Because of the exception the try part isn't run, but the catch part is.
> Did I understand this correctly?

Nope.  If "f = dofoo()" fails and an exception is thrown, that catch block 
is not run, because "f = dofoo()" is outside the try block.  A catch block 
is only run if an uncaught exception occurs in the try block right before 
it.  Since "f = dofoo()" is not in a try block, any exception from it will 
just be thrown out of abc(). 




More information about the Digitalmars-d-learn mailing list