Name

RaiseError ( boolean, inherited)

Synopsis

This attribute can be used to force errors to raise exceptions rather than simply return error codes in the normal way. It is “off " by default. When set to “on”, any method that results in an error will cause the DBI to effectively do a die("$class $method failed: $DBI::errstr"), where $class is the driver class, and $method is the name of the method that failed. For example:

DBD::Oracle::db prepare failed: ... error text here ...

If PrintError is also on, then the PrintError is done before the RaiseError unless no _ _DIE_ _ handler has been defined, in which case PrintError is skipped, since the die will print the message.

If you want to temporarily turn RaiseError off (inside a library function that is likely to fail, for example), the recommended way is like this:

{
  local $h->{RaiseError}; # localize and turn off for this block
  ...
}

The original value will automatically and reliably be restored by Perl, regardless of how the block is exited. The same logic applies to other attributes, including PrintError.

Sadly, this doesn’t work for Perl versions up to and including 5.004_04. For backwards compatibility, you could just use eval { ... } instead.

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.