----- Original Message -----
From: "Bodo Eing" <[EMAIL PROTECTED]>
Subject: RE: executing atomic transactions in DBI>
> 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.

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







Reply via email to