in reply to Re^3: Capturing error thrown by a database
in thread Capturing error thrown by a database

Hi Marshall.. The example which you have given is working fine.. i am extremely thankfull to you for that, but I am using the RaiseError=1 attribute, which causes a die, inside an eval.. Can you give an example similar to above using RaiseError=1.. Thanks a lot..
  • Comment on Re^4: Capturing error thrown by a database

Replies are listed 'Best First'.
Re^5: Capturing error thrown by a database
by Marshall (Canon) on Aug 01, 2011 at 13:06 UTC
    You just check and see if $@ gets set after the eval block. See eval doc page

    eval { some db command $dbh->do....; } if ($@) { print "$@\n"; print $DBI::Errstr,"\n"; }
    do some experimenting, $@ may be same as the dbi's error string if PrintError =1.
      Hey Marshall.. I have already tried that.. I am pasting here the code snippet. Please advise.. eval { sub OpenSybaseConnection { my ($srv, $user, $pwd, $db) = @_; my $dbh; $FH1=$_4; print $FH1 "\n\nConnected to Server:$srv, Database:$db\n"; $dbh=DBI->connect("dbi:Sybase:server=$srv", $user, $pwd,{RaiseError =>1,PrintError =>1 }) || die $DBI::errstr; $dbh->do("use $db"); $common::conn_hdl = $dbh; }; }; if($@) { @proc_error=$@ ; print "inside if\n"; print "Check for if\n"; print "Hi:$@"; print "\nIn Common: @proc_error: HELP"; }
        Please use <code> </code> tags around your code so that the line breaks are preserved. The eval{} goes around the dbi method code! Not around the subroutine! Anyway this is how to use eval... The connect asks to use RaiseError =>1 and I'm not sure that's in effect if the connect fails so I think the extra die is just fine- just in case. If the connect succeeds, the "do" happens next.
        sub OpenSybaseConnection { my ($srv, $user, $pwd, $db) = @_; my $dbh; $FH1=$_4; #???????????????????????????? eval { $dbh=DBI->connect( "dbi:Sybase:server=$srv", $user, $pwd, {RaiseError =>1, PrintError =>1} ) or die "Extra death $DBI::errstr"; $dbh->do("use $db"); }; if($@) # true if the eval failed (die'd at some point) { @proc_error=$@ ; print "inside if\n"; print "Hi:$@"; print "\nIn Common: @proc_error: HELP"; } else # this is the "no error" case { $common::conn_hdl = $dbh; print $FH1 "\n\nConnected to Server:$srv, Database:$db\n"; } return; }
        dupe post for some reason..