Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl DBI抛出哪些异常?_Perl_Dbi - Fatal编程技术网

Perl DBI抛出哪些异常?

Perl DBI抛出哪些异常?,perl,dbi,Perl,Dbi,如果我设置了RaiseError=1,在连接或执行时会引发什么异常 如果我将我的execute()方法放在trycatch中,我应该捕获什么异常呢?异常,因为它们存在于Perl中,是非类型化的,下面是捕获异常的方法: eval { do_something_which_may_throw_exception(); }; if ($@) { print "Exception: $@\n"; }; 简而言之,eval{…}块充当“try”,而if($@){…}充当“catch”,其中异常文

如果我设置了
RaiseError=1
,在连接或执行时会引发什么异常

如果我将我的
execute()
方法放在
try
catch
中,我应该捕获什么
异常呢?

异常,因为它们存在于Perl中,是非类型化的,下面是捕获异常的方法:

eval {
  do_something_which_may_throw_exception();
};
if ($@) {
  print "Exception: $@\n";
};
简而言之,
eval{…}
块充当“try”,而
if($@){…}
充当“catch”,其中异常文本包含在特殊变量
$@

中,因为Perl中存在异常,它们是非类型化的,下面是捕获异常的方法:

eval {
  do_something_which_may_throw_exception();
};
if ($@) {
  print "Exception: $@\n";
};
简而言之,
eval{…}
块充当“try”,而
if($@){…}
充当“catch”,其中异常文本包含在特殊变量
$@
列表中,并解释了许多选项,其中许多选项与错误处理有关

Perl有两种主要的错误处理习惯用法:

  • 返回一个假值。错误的原因在于某个全局变量
  • die
    带有一些错误消息(致命)
  • 默认情况下,DBI使用第一个习惯用法。错误原因在
    $DBI::errstr
    中。要使其工作,必须检查每个DBIAPI调用的返回值

    当您感到懒惰时,可以使用异常。在句柄构造函数中设置
    RaiseError
    ,将使DBI方法抛出异常。从文档中:

    拉塞尔罗 类型:布尔型,继承

    RaiseError
    属性可用于强制错误引发异常,而不是简单地以正常方式返回错误代码。它默认为“关闭”。当设置为“on”时,任何导致错误的方法都将导致DBI有效地执行
    死亡($class$method failed:$DBI::errstr”)
    ,其中
    $class
    是驱动程序类,
    $method
    是失败方法的名称。例如:

    DBD::Oracle::db prepare failed: ... error text here ...
    
    [……]

    通常,
    RaiseError
    eval{…}
    结合使用,以捕获已抛出的异常,然后是
    if($@){…}
    块来处理捕获的异常。例如:

    eval {
      ...
      $sth->execute();
      ...
    };
    if ($@) {
      # $sth->err and $DBI::err will be true if error was from DBI
      warn $@; # print the error
      ... # do whatever you need to deal with the error
    }
    
    在该eval块中,
    $DBI::lasth
    变量可用于诊断和报告,如果您不能确定是哪个句柄触发了错误

    如您所见,Perl中的异常不是用try/catch处理的,而是用
    eval{…}
    处理的。在执行
    eval
    die
    s后,
    $@
    错误变量将设置为该错误,您可以自由处理该错误。请注意,DBI不使用异常对象。

    列出并解释了许多选项,其中许多选项与错误处理有关

    Perl有两种主要的错误处理习惯用法:

  • 返回一个假值。错误的原因在于某个全局变量
  • die
    带有一些错误消息(致命)
  • 默认情况下,DBI使用第一个习惯用法。错误原因在
    $DBI::errstr
    中。要使其工作,必须检查每个DBIAPI调用的返回值

    当您感到懒惰时,可以使用异常。在句柄构造函数中设置
    RaiseError
    ,将使DBI方法抛出异常。从文档中:

    拉塞尔罗 类型:布尔型,继承

    RaiseError
    属性可用于强制错误引发异常,而不是简单地以正常方式返回错误代码。它默认为“关闭”。当设置为“on”时,任何导致错误的方法都将导致DBI有效地执行
    死亡($class$method failed:$DBI::errstr”)
    ,其中
    $class
    是驱动程序类,
    $method
    是失败方法的名称。例如:

    DBD::Oracle::db prepare failed: ... error text here ...
    
    [……]

    通常,
    RaiseError
    eval{…}
    结合使用,以捕获已抛出的异常,然后是
    if($@){…}
    块来处理捕获的异常。例如:

    eval {
      ...
      $sth->execute();
      ...
    };
    if ($@) {
      # $sth->err and $DBI::err will be true if error was from DBI
      warn $@; # print the error
      ... # do whatever you need to deal with the error
    }
    
    在该eval块中,
    $DBI::lasth
    变量可用于诊断和报告,如果您不能确定是哪个句柄触发了错误


    如您所见,Perl中的异常不是用try/catch处理的,而是用
    eval{…}
    处理的。在执行
    eval
    die
    s后,
    $@
    错误变量将设置为该错误,您可以自由处理该错误。请注意,DBI不使用异常对象。

    如果您想从DBI获取正式的异常对象,可以使用
    HandleError
    属性和。我自己用。从大纲中:

    use DBI;
    use Exception::Class::DBI;
    
    my $dbh = DBI->connect($dsn, $user, $pass, {
        PrintError  => 0,
        RaiseError  => 0,
        HandleError => Exception::Class::DBI->handler,
    });
    
    eval { $dbh->do($sql) };
    
    if (my $ex = $@) {
        print STDERR "DBI Exception:\n";
        print STDERR "  Exception Type: ", ref $ex, "\n";
        print STDERR "  Error:          ", $ex->error, "\n";
        print STDERR "  Err:            ", $ex->err, "\n";
        print STDERR "  Errstr:         ", $ex->errstr, "\n";
        print STDERR "  State:          ", $ex->state, "\n";
        print STDERR "  Return Value:   ", ($ex->retval || 'undef'), "\n";
    }
    

    如果您想从DBI获取正式异常对象,可以使用
    HandleError
    属性和。我自己用。从大纲中:

    use DBI;
    use Exception::Class::DBI;
    
    my $dbh = DBI->connect($dsn, $user, $pass, {
        PrintError  => 0,
        RaiseError  => 0,
        HandleError => Exception::Class::DBI->handler,
    });
    
    eval { $dbh->do($sql) };
    
    if (my $ex = $@) {
        print STDERR "DBI Exception:\n";
        print STDERR "  Exception Type: ", ref $ex, "\n";
        print STDERR "  Error:          ", $ex->error, "\n";
        print STDERR "  Err:            ", $ex->err, "\n";
        print STDERR "  Errstr:         ", $ex->errstr, "\n";
        print STDERR "  State:          ", $ex->state, "\n";
        print STDERR "  Return Value:   ", ($ex->retval || 'undef'), "\n";
    }
    

    挑剔:
    eval
    返回最后一条语句的值,或
    undef
    出错。如果要依赖返回值进行错误处理,则应返回显式的真值:
    eval{do something;1}或do{handle\u error()}
    。我认为如果($@){handle_error()}
    @amon极好的一点,最好做一个
    if($@){handle_error()}
    ;我更新了答案以反映它。第一句话的目的是什么?至少有5个模块提供了try/catch,Takkun没有说明使用了哪一个。try::Tiny也提供了try-catch,并且没有被弃用。@anon,
    如果($@{handle_error()}
    直到最近才注意到发生了异常。(5.12?5.14?5.16?)挑剔:
    eval
    返回上一条语句的值或
    undef
    出错。如果要依赖返回值进行错误处理,则应返回显式的真值:
    eval{do something;1}或do{handle\u error()}
    。我认为最好是在($@){handle_error()}@amon优秀的poi中执行
    if($@){handle_error()}