Programmer's notebook: a taxonomy of error handling

Discussion in 'Perl Misc' started by Irving Kimura, Jan 30, 2004.

  1. (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
     
    Irving Kimura, Jan 30, 2004
    #1
    1. Advertising

  2. Irving Kimura

    fishfry Guest

    In article <bvch31$mc6$>,
    Irving Kimura <> wrote:

    > (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.
     
    fishfry, Jan 30, 2004
    #2
    1. Advertising

  3. 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
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
     
    Tassilo v. Parseval, Jan 30, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ej
    Replies:
    3
    Views:
    316
    Francois
    Mar 22, 2006
  2. Calvin Spealman
    Replies:
    0
    Views:
    465
    Calvin Spealman
    Aug 12, 2008
  3. Irving Kimura

    Programmer's notebook: config files

    Irving Kimura, Jan 18, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    137
  4. Irving Kimura
    Replies:
    6
    Views:
    300
    Charles DeRykus
    Feb 3, 2004
  5. kj

    ISO a taxonomy of AJAX errors

    kj, Jan 29, 2008, in forum: Javascript
    Replies:
    2
    Views:
    151
Loading...

Share This Page