Re: Error-handling Question
From: Hardy Merrill (hmerrill_at_redhat.com)
Date: 12/15/03
- Next message: Vassiliy Truskov: "DBD::ODBC 1.06 problem to build 64 bit on HP_UX"
- Previous message: Rudy Lippan: "Re: Error-handling Question"
- In reply to: Unknown Sender: "Error-handling Question"
- Next in thread: Rudy Lippan: "Re: Error-handling Question"
- Reply: Rudy Lippan: "Re: Error-handling Question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 15 Dec 2003 13:29:55 -0500 To: paul.boutros@utoronto.ca
paul.boutros@utoronto.ca [paul.boutros@utoronto.ca] wrote:
> Howdy,
>
> If I'm handling errors with
> eval {} if ($@) {}
> constructs, is there any hidden gotcha to grouping several statements in a
> single eval if I want any errors handled identically? For instance, can I wrap
> an execute and its fetches into one eval, as below, or is this something I
> should be wary of?
>
> Paul
>
> # group an execute and its fetches into one eval
> eval {
> $sth->execute();
> while (my $ref = $sth->fetchrow_arrayref()) {
> print OUT join("\t", @$ref), "\n";
> }
> };
> if ($@) {
> $dbh->rollback();
> close(OUT);
> print "Error retrieving data\n";
> exit();
> }
>
> # wrap the execute and each fetch individually
> eval {
> $sth->execute();
> };
> if ($@) {
> $dbh->rollback();
> close(OUT);
> print "Error retrieving data\n";
> exit();
> }
>
> while (1) {
>
> my $ref;
>
> eval {
> $ref = $sth->fetchrow_arrayref();
> };
> if ($@) {
> $dbh->rollback();
> close(OUT);
> print "Error retrieving data\n";
> exit();
> }
>
> print OUT join("\t", @$ref), "\n";
>
> }
Paul, you normally don't care about committing or rolling
back unless you're actually updating the database by doing
either an INSERT or an UPDATE. When you're just doing
SELECT and fetches you really don't care about transaction
processing. If you want to use eval and $@ to trap errors
with fetches that's fine, but no need to have the rollback
in the if ($@).
I can't really think of any gotchas to be wary of using
eval to wrap several dbi statements.
-- Hardy Merrill Red Hat, Inc.
- Next message: Vassiliy Truskov: "DBD::ODBC 1.06 problem to build 64 bit on HP_UX"
- Previous message: Rudy Lippan: "Re: Error-handling Question"
- In reply to: Unknown Sender: "Error-handling Question"
- Next in thread: Rudy Lippan: "Re: Error-handling Question"
- Reply: Rudy Lippan: "Re: Error-handling Question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]