> -----Original Message-----
> From: Alex Gerasev [mailto:[EMAIL PROTECTED]]
> Sent: Sunday, September 23, 2001 2:21 PM
> To: [EMAIL PROTECTED]
> 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.



>
>   # 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.
>
> Regards,
> Alex Gerasev
> [EMAIL PROTECTED]

Reply via email to