Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

All that I could really offer is “what works for me.”   In pretty much all of my recent code, you’ll find this near the top:

use Try::Tiny; use Exception::Class; use Exception::Caught; use except::classes; use Data::Dumper;

If you search for these at http://search.cpan.org, you will see how each one of these work.

I write code so that it either succeeds or it dies, and when it dies I prefer that it throw an exception-object so that it can carry a “payload” about exactly what happened, without me having to try to deduce it from a text-string.   I also find it useful that, with the mechanisms cited above, I can get the traceback of called routines without at that moment spewing it out to STDERR.   This allows me to, for example, get a truly useful diagnostic message in the Apache logs without, so to speak, throwing up on the web user’s screen.

Specifically:   I set up a hierarchy of exception-classes in one application-wide shared module.   The outer-level loop of the program contains a “catch-all” handler which will intercept any exception that no one else has caught.   Each significant block of code contains a surrounding exception-handling block, and it “re-throws” any hot potato that it does not itself know how to handle.   My code is aggressively and constantly checking itself for errors, testing every assumption with an assertion, with the result that while it will throw a slew of errors during testing, the pattern is that “suddenly, and pretty much forevermore, it stops.”   Each time I am shooting down another bug, I am always able to know with a fairly high degree of precision where to find it.   (Which is, as the credit-card commercials say, “Priceless.”)

Part of the perldoc which I dutifully write for every package/object is a discussion of the exception classes that this object may (directly) throw, and under what conditions it will do so.   The exception is, as it were, “a possible output,” and the object’s contract with its caller is that, if it successfully returns control back to you, at all, then you can rely that it [thought in good faith that it] successfully did what it was told and returned a reliable answer.   The package which contains all of the exception classes, in turn, dutifully contains a description of each one.


In reply to Re: When throw exceptions? by sundialsvc4
in thread When throw exceptions? by Anonymous Monk

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 05:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found