Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Exception Handling In perl

Posted on 2004-10-18
9
Medium Priority
?
504 Views
Last Modified: 2008-02-20
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{

}
0
Comment
Question by:subirc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 

Author Comment

by:subirc
ID: 12344048
Can eval statament do the same thing?

Thanks,
Subirc
0
 
LVL 20

Expert Comment

by:jmcg
ID: 12344693
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.
0
 
LVL 18

Expert Comment

by:kandura
ID: 12345802
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 {
        ...
    };
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:subirc
ID: 12347925
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
0
 
LVL 20

Expert Comment

by:jmcg
ID: 12348298
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.
0
 
LVL 18

Accepted Solution

by:
kandura earned 60 total points
ID: 12348937
Indeed.

You need to do the prepare beforehand, and wrap each execute in an eval. Optionally you could wrap the entire db business in another eval:

    use Error qw(:try);

    eval {
        $sql = qq{Insert into SHIMADZU_METADATA(User_id, Filename, Date_Time) values (?, ?, ?)};
        local $dbh->{RaiseError} = 1;
        my $sth = $dbh->prepare($sql);

        try{
            $sth->execute($UserId, $datafile, $fileDate);
            $sth->commit();
        } catch Error with {
            my $E = shift;
            $sth->rollback;
            warn "Error committing row: $E";
        };
    } or warn "Error with insert: $@";
0
 

Author Comment

by:subirc
ID: 12349129
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
0
 

Author Comment

by:subirc
ID: 12349177
Think it's a diff error..... can't able to see commit........I tried  commit()....but still failed......
0
 

Author Comment

by:subirc
ID: 12349302
got it;  it  shld be  $dbh->commit   not  $sth...................................!!!!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question