简体   繁体   English

如何处理Perl中模块抛出的错误

[英]How to handle error thrown by module in perl

I am using the module DBD::Oracle in perl to insert xml contents into oracle 11 g instance. 我在perl中使用模块DBD::Oracle将xml内容插入到Oracle 11 g实例中。 While inserting some of the documents in my sample set the script fails as the module returns Unsupported named object type for bind parameter . 在示例集中插入一些文档时,脚本将失败,因为该模块Unsupported named object type for bind parameter返回Unsupported named object type for bind parameter I would like to handle this error and make the loop iteration to go on. 我想处理此错误并使循环迭代继续进行。

following is my code, 以下是我的代码,

use strict;
use warnings;
use DBI;
use DBD::Oracle qw(:ora_session_modes);
use DBD::Oracle qw(:ora_types);

die("USAGE: $0 <input_directory>") unless ($#ARGV == 0);
my $directory=$ARGV[0];

my $dbh = DBI->connect('dbi:Oraclle:dbname',"username", "pass");
my $SQL;



opendir(IMD, $directory) || die ("Cannot open directory");
my @listOfFiles= readdir(IMD);
closedir(IMD);

my $xmltype_string;
my $xml;
my $i = 1; 
foreach my $file(@listOfFiles)
{
    unless($file eq '.' or $file eq '..')
    {
        print "inserting File no. $i \t $file .... \n";

        {
                local $/=undef;
                open (FILE , "<" , "$directory/$file" );
                $xml=<FILE>;
                close (FILE);
        }
        $SQL="insert into sampleTable values ( :ind, :xml)";
        my $sth =$dbh-> prepare($SQL);
        $sth->bind_param(":xml" , $xml , { ora_type => ORA_XMLTYPE});
        $sth->bind_param(":ind" , $i);
        $sth-> execute();


        $i++;
    }
}

Am getting the error in bind param. 正在获取绑定参数错误。

Error handling is usually done via the Try::Tiny module: 错误处理通常是通过Try::Tiny模块完成的:

use Try::Tiny;

try {
    something_that_could_die();
}
catch {
    handle_error($_);
}
finally {
    do_something_either_way();
}; # ← trailing semicolon not optional.

Both catch and finally are optional. catchfinally都是可选的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM