Automatic error checking
The automatic
error checking capabilities of the DBI operates on two levels. The
PrintError
handle attribute tells DBI to call the
Perl warn( )
function (which typically
results in errors being printed to the screen when encountered) and
the RaiseError
handle attribute (which tells DBI
to call the Perl die( )
function upon error, typically
causing the script to immediately abort).
Because the standard Perl functions of warn( )
and
die( )
are used, you can change the effects of
PrintError
and RaiseError
with
the $SIG{_ _WARN_ _}
and $SIG{_ _DIE_ _}
signal handlers. Similarly, a
die( )
from RaiseError
can be
caught via eval { ... }
.
These different levels of automatic error checking can be turned on for any handle, although database handles are usually the most common and useful. To enable the style of automatic error checking you want, you may set the value of either of the following two attributes:
$h->{PrintError} = 1; $h->{RaiseError} = 1;
Similarly, to disable
automatic error checking, simply set the value of these attributes to
0
.
If both RaiseError
and
PrintError
are enabled, an error will cause
warn( )
and die( )
to be
executed sequentially. If no $SIG{_ _DIE_ _}
handle has been defined, warn( )
is skipped to
avoid the error message being printed twice.[39]
A more common way in which these attributes are used is to specify
them in the optional attribute hash supplied to
DBI-
>connect( )
when connecting to a database. Automatic error checking is the ...
Get Programming the Perl DBI now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.