When you create your database connection use the 4th arg to change
connection behaviour. Specifically, tell it to throw exceptions when
they occur so that you can trap them with an eval;

eval {
    my $dbh = DBI->connect($connstr, $user, $pass, {RaiseError => 1});
    my $stmt = $dbh->prepare($select_sql);
    $stmt->execute();
    while (my @row = $stmt->fetchrow_array()) {
    }
};
if ($@) {
    # error message, if one occurs, will now be in $@
    # for any error thrown by DBI
}

-----Original Message-----
From: Parag Kalra [mailto:[email protected]] 
Sent: Monday, November 09, 2009 2:37 AM
To: [email protected]
Subject: How to catch the returned error message.

Hello All,

This is my first post here.

I am executing a Perl script which makes use of 'DBI' module.

I am performing a select operation on a table through the Perl script
and I
am getting following error on the console:

DBD::ODBC::st execute failed: [Oracle][ODBC][Ora]ORA-00942: table or
view
does not exist
 (SQL-42S02) at Test.pl line 67

The error message is well justified as the table doesn't exist.

However I just wanted to know if there is a way to catch this error
message
through some variable so that I can print it in a log file as well.

Cheers,
Parag

Reply via email to