簡體   English   中英

如何處理Perl中模塊拋出的錯誤

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

我在perl中使用模塊DBD::Oracle將xml內容插入到Oracle 11 g實例中。 在示例集中插入一些文檔時,腳本將失敗,因為該模塊Unsupported named object type for bind parameter返回Unsupported named object type for bind parameter 我想處理此錯誤並使循環迭代繼續進行。

以下是我的代碼,

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++;
    }
}

正在獲取綁定參數錯誤。

錯誤處理通常是通過Try::Tiny模塊完成的:

use Try::Tiny;

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

catchfinally都是可選的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM