How do I separate out exception handling / error handling from business logic? I'm writing code in Perl, and the error/exception handling and business logic makes it very difficult to understand code while reviewing.
How can I refactor my code to make it more readable yet have error handling. Also note that I do not use try catch or anything like that.
One of our senior programmers suggested that we re open OS standard error and write everything there and we can catch it by the caller.
Edit : here is how I do the error handling . I have many Perl modules..
so check2.pm
package check2;
sub printData {
print STDERR "Error Message from sub routine \n";
}
1;
and i use it like so in my Perl script , check.pl
In my Perl script
#!/usr/bin/perl
use LoadModules;
use strict;
use warnings;
load check2;
my $stderrholder;
local *SAVEERR;
# First, save existing STDERR
open SAVEERR, ">&STDERR" or print "not able to open";
close STDERR;
# Open the STDERR to a variable
open STDERR, ">", \$stderrholder or die "Failed to reopen STDERR $!\n";
#Start of processing
# Now print something to STDERR, redirected to $ stderrholder
print STDERR " Error Message from Main script \n";
check2::printData();
#End of processing
# Now close and restore STDERR to original condition.
close STDERR;
#open STDERR, ">&SAVEERR";
# Now check if there were any processing errors.
if(length($stderrholder)) {
print "\nProcessing errors\n" ;
if(length($stderrholder)) {
print "\nProcessing errors\n" ;
print $stderrholder;
} else {
print "\nNo Processing errors\n" ;
}
Would really appreciate if someone could help me point out errors in this.