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

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.

Replies are listed 'Best First'.
Re^6: Capturing error thrown by a database
by puneet.keswani (Novice) on Aug 01, 2011 at 13:33 UTC
    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; }
        Wao Marshall... Thanks a lot.. My purpose is solved and u were a great help to me.. I was stuck with this issue for quite sometime now... Thanks to you that my problem is solved..
      dupe post for some reason..