Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to catch the returned error message.

6 views
Skip to first unread message

Parag Kalra

unread,
Nov 9, 2009, 5:37:29 AM11/9/09
to dbi-...@perl.org
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

Tim Tisdall

unread,
Nov 9, 2009, 9:22:44 AM11/9/09
to Parag Kalra, dbi-...@perl.org
If you have something like $dbh->do(); You can get the error code
with $dbh->err and the full error message with $dbh-errstr .

for more info:
http://search.cpan.org/~timb/DBI/DBI.pm#err

Erick Nelson

unread,
Nov 9, 2009, 10:20:02 AM11/9/09
to Parag Kalra, dbi-...@perl.org
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

Parag Kalra

unread,
Nov 9, 2009, 1:11:05 PM11/9/09
to dbi-...@perl.org
Thanks to All...

It did the trick -
http://search.cpan.org/~timb/DBI/DBI.pm#$DBI::errstr<http://search.cpan.org/%7Etimb/DBI/DBI.pm#$DBI::errstr>

Cheers,
Parag


On Mon, Nov 9, 2009 at 7:52 PM, Tim Tisdall <tis...@gmail.com> wrote:

> If you have something like $dbh->do(); You can get the error code
> with $dbh->err and the full error message with $dbh-errstr .
>
> for more info:

> http://search.cpan.org/~timb/DBI/DBI.pm#err<http://search.cpan.org/%7Etimb/DBI/DBI.pm#err>

Jared Still

unread,
Nov 11, 2009, 4:34:00 PM11/11/09
to Parag Kalra, dbi-...@perl.org
In addition to the advice you have received, I might
suggest that you read all of the user documentation
for both DBI and DBD::Oracle.

You'll be glad you did.

It is a good idea to review it periodically as well.

Which reminds me ...

Jared Still
Certifiable Oracle DBA and Part Time Perl Evangelist
Oracle Blog: http://jkstill.blogspot.com
Home Page: http://jaredstill.com

0 new messages