dbi-methods_of_error_handling

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



To: <dbi-users@perl.org>
From: "Alex Gerasev" <alex@gerasev.com>
Subject: RE: Avoid Error if no result
Date: Mon, 24 Sep 2001 00:21:17 +0600

In regard to what is lately being discussed in this mail list I would like
to offer two ways of handling DBI errors which, in my opinion, are flexible
and powerful enough to resolve any issues:

Way 1:

$sth = $dbh->prepare(...);
if($DBI::err){
  print "$DBI::err $DBI::errstr";
}

I think it is better to explicitly check for the value of $DBI::err because
it is guaranteed that this value will be false ONLY when there was no error
in the previous DBI operation.  Otherwise error may be confused with no
lines fetched, for example.  In the above  case you would probably want to
use RaiseError => 0, PrintError => 0 because you'll provide your own error
handling procedures.

Way 2:

# Replace __WARN__ signal handler
$SIG{__WARN__} = sub


  # We're interested in database errors only
  if($DBI::err){
    print "<h2>Database said: $DBI::errstr. What the heck???</h2>";
  }
};

Replace handler for the __WARN__ signal.  This is pretty nice way because
you don't have to check for $DBI::err after all DBI operations.  Actually,
after you have replaced signal handler the rest of your code may look like
it has no error handling procedures at all - they will be executed
automatically.  Inside the signal handler you'll be able to pay attention
only to certain errors or print HTML messages to user's browser, for
example.  Just be sure to handle WARN signals that are not caused by DBI
errors properly.

===

To: alex@gerasev.com, dbi-users@perl.org
From: "Sterin, Ilya" <Isterin@ciber.com>
Subject: RE: Avoid Error if no result
Date: Sun, 23 Sep 2001 14:20:50 -0400

> -----Original Message-----
> From: Alex Gerasev [mailto:alex@gerasev.com]
> Sent: Sunday, September 23, 2001 2:21 PM
> To: dbi-users@perl.org
> Subject: RE: Avoid Error if no result
>
>
> In regard to what is lately being discussed in this mail list I would like
> to offer two ways of handling DBI errors which, in my opinion,
> are flexible
> and powerful enough to resolve any issues:
>
> Way 1:
>
> $sth = $dbh->prepare(...);
> if($DBI::err){
>   print "$DBI::err $DBI::errstr";
> }

Actually the same will happen if you use PrintError => 1.


>
> I think it is better to explicitly check for the value of
> $DBI::err because
> it is guaranteed that this value will be false ONLY when there
> was no error
> in the previous DBI operation.  Otherwise error may be confused with no
> lines fetched, for example.  In the above  case you would probably want to
> use RaiseError => 0, PrintError => 0 because you'll provide your own error
> handling procedures.
>
> Way 2:
>
> # Replace __WARN__ signal handler
> $SIG{__WARN__} = sub
>

You can also just use RaiseError => 1 and define a __DIE__ handler, based on
the contents of $DBI::err and $DBI::errstr.


===


the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu