Link to home
Start Free TrialLog in
Avatar of subirc
subirc

asked on

Exception Handling In perl

Is it possible to have exception handling in perl with try catch loop?  I  am using DBI and is inserting data from a csv into a database inisde a while loop. Can I use try catch loop instead of  eval {.........}; if($@){.....}   commands.  I want that if a insertion fails that the process should not stop and should continue after throwing an excpetion till the end of the CSV.  

I am trying to use the error module but it didn't work.......
use Error qw(:try);
try{
    $sql = qq{Insert into SHIMADZU_METADATA(User_id, Filename, Date_Time) values (?, ?, ?)};
    $sth = $dbh->prepare($sql) || die;
    $sth->execute($UserId, $datafile, $fileDate);
    $sth->commit();
}catch{

}
Avatar of subirc
subirc

ASKER

Can eval statament do the same thing?

Thanks,
Subirc
Yes, the eval statement can be used. The 'try' function from that module is mostly just syntactic sugar built around 'eval'.

You haven't explained "it didn't work" in enough detail for experts to be able to give you much of an answer. Please elaborate.
jmcg is quite right. Eval will do just fine. In either way I'd recommend you turn on RaiseError on the database handle, at least for the duration of the try block:
That way it dies whenever there's an error, not just on the prepare (in fact, the execute statement is a lot more dangerous).

    eval {
        local $dbh->{RaiseError} = 1;

    };

   
FYI, the Error syntax is:

    try {
        ...
    } catch Error with {
        my $Err = shift;
        ...
    };

or

    try {
        ...
    } otherwise {
        ...
    };
Avatar of subirc

ASKER

jmcg,

"it didn't work" means that once a particular row failed and threw an error ....the script stopped running and nothing following got transmitted..................

Subirc
Just off the top of my head, I think you'll need to arrange to insert one row at a time. This will allow you to handle and dismiss the exception when it occurs, then continue. I don't know of a way to both have the error reported and to continue processing after the error.
ASKER CERTIFIED SOLUTION
Avatar of kandura
kandura

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of subirc

ASKER

ya I am doing one row at a time

use strict;
use DBI qw(:sql_types);
eval{
      local $dbh->{RaiseError} = 1;      #added as per kandura's suggestion
      $sth->execute($UserId, $datafile, $fileDate);
      $sth->commit();
};
if ($@) {
    print $@
}

Here's the error.......
Can't locate object method "Commit" via package "DBI::st" (perhaps you forgot to load "DBI::st"?) ...never seen this error
Avatar of subirc

ASKER

Think it's a diff error..... can't able to see commit........I tried  commit()....but still failed......
Avatar of subirc

ASKER

got it;  it  shld be  $dbh->commit   not  $sth...................................!!!!