CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
[SOLVED] Perl DBI - Error handling

 



terrykhatri531
User

Aug 29, 2014, 10:02 AM

Post #1 of 7 (373 views)
[SOLVED] Perl DBI - Error handling Can't Post

Hi,

I have a little query utility web page where you can execute your queries, I am trying to capture error so that I can present it in a better way if something goes wrong with the execution, here's how I am trying to capture it:


Code
  
.....................

$sth->execute();
if ( $sth->err )
{
die "There is something wrong with your query :" . $sth->err . " error msg: " . $sth->errstr . "\n";
}


But instead of display my msg its showing the following error:


Code
Software error:Can't call method "execute" on an undefined value at /usr/share/nwind/cgi-bin/viewquerybuilder.pl line 91.  

For help, please send mail to the webmaster (root@localhost), giving this error message and the time and date of the error.


Many thanks for your help !

Terry


(This post was edited by terrykhatri531 on Aug 30, 2014, 2:51 AM)


Zhris
Enthusiast

Aug 29, 2014, 10:15 AM

Post #2 of 7 (370 views)
Re: [terrykhatri531] Perl DBI - Error handling [In reply to] Can't Post

Your error is caused because $sth is undefined, which occurs before the if statement that tests if there was a statement error. There should never be a circumstance under which you try to use an undefined statement handler, you should have died beforehand.

You can trap fatal errors within an eval block, if an error occurs it will be available in the special variable $@ (or $EVAL_ERROR if using English):


Code
eval 
{
# some expression
};
if ( my $error = $@ )
{
# do something with $error.
}


Note there can be issues with the preservation of $@ etc, Try::Tiny handles error trapping more reliably.

Finally, there is excellent information regarding handling DBI errors here.

Chris


(This post was edited by Zhris on Aug 29, 2014, 11:47 AM)


terrykhatri531
User

Aug 29, 2014, 10:50 AM

Post #3 of 7 (359 views)
Re: [Zhris] Perl DBI - Error handling [In reply to] Can't Post

Hi Crhis,

I tried :


Code
 
eval {
$sth->execute();
};
if ( my $error = $@ )
{
warn "There is a problem with your query, error msg is : $error\n";
}




Still the same Software Error is being printed.

Am I doing it right ?

Rgds.

Terry


Zhris
Enthusiast

Aug 29, 2014, 10:54 AM

Post #4 of 7 (354 views)
Re: [terrykhatri531] Perl DBI - Error handling [In reply to] Can't Post

Thats right, its telling you there is a problem that you need to fix, the problem being that $sth is undefined.


Quote
I am trying to capture error so that I can present it in a better way


The eval suggestion was with consideration to this. Its common to check what $@ contains, possibly using a regular expression / translate to a more meaningful response. You can also adjust local $SIG{__DIE__} = sub { ... } to hook dies within local scope through your own handler.

Chris


(This post was edited by Zhris on Aug 29, 2014, 11:01 AM)


terrykhatri531
User

Aug 29, 2014, 11:05 AM

Post #5 of 7 (349 views)
Re: [Zhris] Perl DBI - Error handling [In reply to] Can't Post

Thanks for the link Chris, it works now, the following code prints the Oracle errors now showing what exactly causing the error.


Code
 
my $sth=$dbh->prepare("$string") or die "There is something wrong with your query: $DBI::errstr\n";

eval {
$sth->execute();
};
if ( my $error = $@ )
{
warn "There is a problem with your query, error msg is : $error\n";
}


Rgds.

Terry


Zhris
Enthusiast

Aug 29, 2014, 11:33 AM

Post #6 of 7 (346 views)
Re: [terrykhatri531] Perl DBI - Error handling [In reply to] Can't Post


Quote
code prints the Oracle errors now showing what exactly causing the error


You have fixed your original problem by ensuring $sth is defined via prepare / died if preparation error.

If I were to rewrite your code snippet, assuming RaiseError => 0, I personally think the following would be more appropriate:


Code
my $sth; 
eval
{
$sth = $dbh->prepare($string) or die "failed to prepare: $dbh->errstr\n";
$sth->execute() or die "failed to execute: $sth->errstr\n"; ;
};
if ( my $error = $@ )
{
warn "database error: $error\n";
}


It makes sense to relocate the prepare inside the eval and die with errors whenever possible.

Chris


(This post was edited by Zhris on Aug 29, 2014, 11:58 AM)


terrykhatri531
User

Aug 30, 2014, 2:50 AM

Post #7 of 7 (312 views)
Re: [Zhris] Perl DBI - Error handling [In reply to] Can't Post

Thanks Chris !!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives