perlcritic提示下面的代码,一些工作得很好的样板 DBI 东西,应该 croak
而不是die
:
# Connect to database
my $db_handle = DBI->connect( $url, $user, $password ) or die $DBI::errstr;
所有这一切,虽然 die 似乎对我来说工作得很好。
我认为对于一个武士 Perl 战士来说,当事情出错时,呱呱叫比实际死亡更不光荣。笑话分开
为什么我应该呱呱
而不是死
?
不注意会有什么后果perlcritic's建议?
最佳答案
来自http://www.perlmonks.org/?node_id=685452
You use die when the error is something you or your code didn't do right. You use croak when it's something your caller isn't doing right. die "error: $!" indicates the error is on the line where the error occured. croak "error: $!" indicates the error is on the line where the caller called your code.
在这种情况下,错误(与数据库的连接错误)与调用者无关,而与建立连接的线路有关,因此我将使用 die
。
关于perl - 当 Perl 中发生不好的事情时,是 croak() 还是 die() 更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4156391/