From: "Michael A Mayo" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Subject: Re: executing atomic transactions in DBI
Date sent: Mon, 7 May 2001 14:27:40 -0400
Michael,
> > if the commit() is placed here, won't it be always executed, because
> > there are no die statements which stop code execution upon errors
> > before ? Shouldn't committing be made dependent of the value of $@
> > like
> >
> > if ($@) {
> > warn "Transaction aborted because $@";
> > $dbh->rollback; # undo the incomplete changes
> > # add other application on-error-clean-up code here
> > }
> > else {
> >
> > $dbh->commit();
> > }
>
> No. There is never any need to use an else {} clause after if ($@). It's
> redundant.
>
> I think that part of reason for the misunderstandings in this thread might
> be that a lot of people don't fully understand exception handling.
Exactly (including myself). And that's why threads like this should be kept alive
and *on* list until the basic problem becomes apparent.
> A simple example:
>
> use strict;
>
> sub testdie {
> die "\mthe execution halts here\n";
> }
>
> my ($a, $b) = (1, 1);
> eval {
> $a = 10;
> testdie(); # note: exceptions travel up until they are caught
> $b = 10;
> };
> if ($@) { print $@; }
>
> print "\n a=$a, b=$b\n";
>
> This prints:
> the execution halts here
>
> a=10, b=1
>
>
> There is never any need to use an else {} clause after if ($@). Any
> such clause could just be appended to the end of the eval {} block.
>
> Or, think about it another way: if your else {} clause executes, it means
> there was no exception caught. If there was no exception caught, every
> statement in the original eval {} was executed. In which case, you may as
> well stick the stuff that's in the else {} inside the eval {}.
>
> -Mike
Thanks to you all those who know for their patience...
Bodo
[EMAIL PROTECTED]