DBI: raiseerror in eval

对着背影说爱祢 提交于 2019-12-04 15:51:39

If failed for some reasons, most $dbh methods will either:

  • (if RaiseError option is set to 0) return undef
  • (if RaiseError option is set to 1) immediately exit the script ('die') with error reason given as exit message.

The key point here is that it's up to you how to process the errors. If you wish, you can just ignore them, for example (the following obviously will work with RaiseError set to 0 only):

for my $db ( ... ) {
    my $dbh = get_database_handle( $db )
       or next;
    ...
}

In this snippet (copied from @ikegami's answer you've mentioned in your question) you loop through some list of settings for DB connection; if some connection gives you an undef, you just go for another one, and do nothing with error.

Usually, though, you have to do more than just 'nexting' when error happens - but then again, you have two choices: either check each $dbh-related statement with something like this:

$sth = $dbh->prepare('some_params') 
  or process_db_error('In prepare');
...
$res = $sth->execute('another_set_of_params') 
  or process_db_error('In execute');
...
$res->doAnythingElse('something completely different') 
  or process_db_error('In something completely different');

(as or parts will be executed only if their corresponding 'left parts' evaluate to false in Boolean context).

...or just wrap all this into Perlish 'try-catch' block:

if (!eval {    
   $sth = $dbh->prepare('some_params');
   ...
   $res = $sth->execute('another_set_of_params');
   ...
   $res->doSomethingElse('something completely different') 
   ...
   1  # No exception
}) {
   process_db_error($@);
}

What's to choose, it's up to you: it's a common decision between 'errors in return statements' (except that to fetch an actual error you have to ask $dbh object) and, well, exceptions.

But the bottom line is you cannot write just this:

$sth = $dbh->do_something('that_can_result_in_error');
$sth->do_something('else');

... if you did set RaiseError to 0. In this case script won't die, $sth will be assigned an undef, and you get yourself a 'derivative' error (as you cannot call a method on undef).

And that exactly what happened in the last part of code in your original question.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!