Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

If i wanted to use the following code to handle database errors (with RaiseError = 1)

eval { $dbh->do(...); $dbh->do(...); $dbh->do(...); $dbh->do(...); } if($@) { handle_error(); }
How would i get the error raised by the database in the if statement so I can include it in a custom error message?

Many thanks

Replies are listed 'Best First'.
Re: how do i get the database error from this code
by Corion (Patriarch) on Jan 04, 2011 at 17:08 UTC

    Have you looked at DBI and what it says about errors?

      Yes i've read all of the DBI page but i'm a perl beginner and don't really know how to use the eval structure. I've read about setting RaiseError = 1/0. I want to use the automatic error handling from setting raise error = 1. However i don't know how to 'catch' the die in an eval loop so I can customise the error message for the end user . Will the error message raised by the dbi simply be in the $@ variable so I can work with that?

        Will the error message raised by the dbi simply be in the $@ variable so I can work with that?

        Yes. $dbh->err and the like should also be populated.