我正在编写一个Perl脚本,无论我尝试了什么,我似乎都无法捕捉DBI错误。我试过这个:
use DBI;
$db = DBI->connect("dbi:ODBC:Driver={SQL Server};Server=localhost;DATABASE=nodepoint;UID=sa;PWD=test;") or print "Something happened.";这是:
use DBI;
eval
{
$db = DBI->connect("dbi:ODBC:Driver={SQL Server};Server=localhost;DATABASE=nodepoint;UID=sa;PWD=test;");
};
if ($@) { print "Something happened."; }两者都无法捕捉到错误,相反,我在屏幕上看到了这样的信息:
DBI connect('Driver={SQL Server};Server=localhost;DATABASE=nodepoint;UID=sa;PWD=test','',...) failed: [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (SQL-08001) [state was 08001 now 01000]
[Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (SQL-01000) at C:\dev\test.pl line 5.这是一个大问题,因为当在IIS上使用时,当它看到错误时会抛出一个500.2 Bad Gateway。我需要抓住它,这样我才能显示一个正确的信息。
发布于 2014-12-02 21:42:31
默认的错误处理是:
RaiseError => 0
PrintError => 1
PrintWarn => 0您想将PrintError => 0传递给connect。
如果您喜欢检查错误:
my $dbh = DBI->connect($dsn, $user, $passwd, {
RaiseError => 0,
PrintError => 0,
});
if (!$dbh) {
die($DBI::errstr);
}如果您希望引发异常:
my $dbh = eval {
DBI->connect($dsn, $user, $passwd, {
RaiseError => 1,
PrintError => 0,
})
};
if (!$dbh) {
die($@);
}https://stackoverflow.com/questions/27259174
复制相似问题