Programmer's notebook: a taxonomy of error handling

I

Irving Kimura

(Note to the impatient: The connection with Perl doesn't emerge
until the end of this post.)

I find that nothing complicates and obfuscates my code more than
the problem of dealing with errors: user errors, system errors,
algorithm-design errors...

Just checking for the occurrence of errors and doing something dumb
like aborting the program is bad enough, but trying to make my code
fault-tolerant and/or make it generate reasonably helpful error
messages, turns my code into a horror show. (If on top of this I
add CGI as the context, I begin to contemplate seppuku as the only
way to avoid disgrace.)

The truth of the matter is that I have yet to find a good way to
architect my error handling. Granted, there are many types/levels
of error (as mentioned above) and handling each of these may require
an entirely different architecture. It doesn't matter: I haven't
hit upon a good formula for any of them.

Now I'm at the stage of learning good Perl programming, which
includes reading good Perl source, so I would like to study examples
of Perl modules (from CPAN) with well-designed error-handling
schemes.

Any recommendations would be greatly appreciated.

Irv
 
F

fishfry

Irving Kimura said:
(Note to the impatient: The connection with Perl doesn't emerge
until the end of this post.)

I find that nothing complicates and obfuscates my code more than
the problem of dealing with errors: user errors, system errors,
algorithm-design errors...

Just checking for the occurrence of errors and doing something dumb
like aborting the program is bad enough, but trying to make my code
fault-tolerant and/or make it generate reasonably helpful error
messages, turns my code into a horror show. (If on top of this I
add CGI as the context, I begin to contemplate seppuku as the only
way to avoid disgrace.)

The truth of the matter is that I have yet to find a good way to
architect my error handling. Granted, there are many types/levels
of error (as mentioned above) and handling each of these may require
an entirely different architecture. It doesn't matter: I haven't
hit upon a good formula for any of them.

Now I'm at the stage of learning good Perl programming, which
includes reading good Perl source, so I would like to study examples
of Perl modules (from CPAN) with well-designed error-handling
schemes.

Any recommendations would be greatly appreciated.

The best I've seen is the concept of try/catch/throw, borrowed from Java
and C++. I think there's a library that implements the concept of
exceptions in perl. The idea is that when an error occurs, a piece of
code (method, subroutine, etc.) can throw an error of a particular type.
The caller can elect to catch the error, or if not, control reverts up
the call stack to the first caller who has elected to catch that error.

In this way each individual module only handles certain errors, and
passes others on up. So each module can decide how much error handling
and what type of recovery procedure, if any, to implement.
 
T

Tassilo v. Parseval

Also sprach Irving Kimura:
(Note to the impatient: The connection with Perl doesn't emerge
until the end of this post.)

I find that nothing complicates and obfuscates my code more than
the problem of dealing with errors: user errors, system errors,
algorithm-design errors...

Just checking for the occurrence of errors and doing something dumb
like aborting the program is bad enough, but trying to make my code
fault-tolerant and/or make it generate reasonably helpful error
messages, turns my code into a horror show. (If on top of this I
add CGI as the context, I begin to contemplate seppuku as the only
way to avoid disgrace.)

The truth of the matter is that I have yet to find a good way to
architect my error handling. Granted, there are many types/levels
of error (as mentioned above) and handling each of these may require
an entirely different architecture. It doesn't matter: I haven't
hit upon a good formula for any of them.

Now I'm at the stage of learning good Perl programming, which
includes reading good Perl source, so I would like to study examples
of Perl modules (from CPAN) with well-designed error-handling
schemes.

If you are willing to look at a very huge module, then I recommend Mark
Overmeer's Mail::Box. In his class hierarchy every package inherits from
Mail::Reporter which implements error and warnings handling with a
pretty sophisticated system of priorities which decides whether a
particular warning/error is ignored or shown.

This approach however might not be applicable under all circumstances.
Sometimes you need something different. What you called dumb (namely
aborting the program when an error has been detected) might not be that
dumb really. When properly documented, you give the user a chance to
catch the error with a BLOCK-eval. Throwing exceptions happens through
die() or - more preferably - through Carp::croak(). In either case you
can have them throw objects instead of error-strings. The thrown object
ends up in $@:

sub method_that_potentially_croaks {
my $self = shift;
....
if ($error) {
my $err_obj = MyError->new( "Something went wrong", __LINE__, ...);
Carp::croak $err_obj;
}
...
}

And somewhere else where you use the module that contains the above:

eval {
$obj->method_that_potentially_croaks;
}
if ($@) {
print "Error: ", $@->as_string;
print " at ", $@->lineno;
etc.
}

Tassilo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top