It is difficult to be certain without having seen any of your code, but it looks as if you are printing your error messages, rather than returning them from your subroutine and saving them in a variable for further handling.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
| [reply] [d/l] [select] |
I am actually using "out" parameters to return the error messages and capturing it in the main perl script with the help of func(syb_out_params), but this is for the user defined error messages.. My problem is what about the error messages which will not be handled by the subroutene, take for example "password has expired" while connecting to the database then database connection will not be possible and database will throw its own error.. How to catch these types of errors..
| [reply] |
When the DBI has an error, it is available as the string provided by DBI->errstr. So for example in a wild guess at your subroutine below, if the connection fails (no handle returned), the sub returns the DBI->errstr. Of course your calling program would have to do something with that. PrintError=1 causes warning message to be printed, you may or may not want that. RaiseError=1 is a bit stronger and causes a "die", which you don't want unless you inside of a block eval.
You can set RaiseError =1 and put statements in an eval{} which in this context would be Perl's equivalent of a try/catch. In the case of failure, again, calling the errstr method of the DBI will give you its error.
my $dbh;
sub connectsybase
{
my ($var1, $var2,...) = @_;
$dbh = DBI->connect($data_source, $username, $auth,
{printError => 1} );
return (DBI->errstr) if (!dbh); # fail
return ""; # success, NULL string
}
| [reply] [d/l] |
Take a look at DBI and it's error-related attributes.
use DBI ;
my $dbh = DBI->connect($data_source, $username, $auth);
$dbh->{PrintError} = 1 ;
$dbh->{RaiseError} = 1 ;
| [reply] [d/l] |
I have used the attributes but without much luck.. while trying to connect to database, I have also tried wrapping the code within eval but again its not helping. If you want I can paste the code..
| [reply] |
Posting your code would help. You can define a subroutine to handle all of your DBI errors: HandleError.
| [reply] [d/l] |