Now that I'm becoming a wiser and more experienced perl hacker, I'm learning that good error handling systems are a bit of an art form. The book "Practice of Programming" recommends to do something like "detect errors at a low level, deal with them on a high level". This is a philosophy I can appreciate. To illustrate my discussion, here's a error handling subroutine that could be used by someone checking the return values from a Postgres insert from a CGI form:
sub is_pg_date_error { my $key = shift; my $value = param($key); if ($DBI::errstr =~ m/Bad date external representation '$value'/o) + { error(title=>'Inproper date format', msg=>"There is a problem with $key field. Please return an +d format it in the format YYYY-MM-DD and confirm that it is a valid da +te.") } }
This is probably bad style, because it detects the error it a low level (where this routine is hidden away), but it doesn't allow the error to handled at a high level-- it throws an error itself immediately. The issue here is that most of the time I do want to throw this error. So I want to be able to handle the error at a high level, but for it happen automatically if I want to.

Looking at how CGI.pm and DBI.pm deal with errors, they both have similiar approaches which are inline with the "Practice of Programming" principles-- when there is an error, they populate some global routines in their package with the relevent errors. You can choose to do something with these errors or just ignore them.

So, when developing custom modules, would recommend the same error handling scheme in general? That is, when there is an error, instead of dealing with directly or returning it to user, stick it into a package global which the user can manipulate on their own? If not that, what other insights on the art of error handling do you have? Thanks!

-mark


In reply to The art of error handling by markjugg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.