解决方法
在DBI-> connect中使用RaiseError => 1配置,并在try块中包含对$dbh和$sth的调用(
TryCatch和
Try::Tiny是try块的良好实现).
有关其他可用连接变量的更多信息,请参见the docs.
例如:
use strict; use warnings; use DBI; use Try::Tiny; my $dbh = DBI->connect( $your_dsn_here,$user,$password,{ PrintError => 0,PrintWarn => 1,RaiseError => 1,AutoCommit => 1,} ); try { # deliberate typo in query here my $data = $dbh->selectall_arrayref('SOHW TABLES',{}); } catch { warn "got dbi error: $_"; };