Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Generally, it's a good idea to handle errors with block eval. In other words, if you do this:

local $@; eval { # My code that can throw an error here might_throw_error(); }; if (my $exception = $@) { # Catch any error here. Error message in $@ warn "Caught random error: $exception\n"; } sub might_throw_error { # Flip a coin and throw an error if ( rand(1) > 0.5 ) { die "Random error!\n"; } }

then you can put your error-handing and logging code in the "if" statement below. It will occasionally warn "Random error! at line X." If you want to rethrow the error, you can put "die" in the if statement.

I generally don't think it's a good idea to call exit() in a constructor, or in anything other than the program's main routine. The best way to handle an error is to call die(). Then, if upper-level routines can handle the exception, they can do so with block eval as above. If you call exit(), there's no catching the exception. It makes it much more difficult to write unit tests for your code, for one thing.

Is the error you're getting pointing to your own code, or code within CGI::Session? If it's your own code, you should use 'ref' to check to see if the variable you have is a string (which may be 'ERROR') or an object, then either renew the session or throw a more descriptive exception yourself with die(). Otherwise, you might want to use eval {} as above to catch the exception and handle it as you see fit.

This is all pretty generic, since I'm not terribly clear on what's causing the exception. You might want to read Chapter 8 of chromatic's Modern Perl, which covers exceptions.

Update:As Anonymonk points out below, block-eval error handling in perl has a number of edge cases. If possible you should use one of the many try-catch CPAN modules available. In addition to the ones he/she names, there's also TryCatch and Throwable.


In reply to Re: A couple questions about object oriented Perl programming: inheritance and error handling by stephen
in thread A couple questions about object oriented Perl programming: inheritance and error handling by ted.byers

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (4)
    As of 2017-12-16 10:24 GMT
    Find Nodes?
      Voting Booth?
      What programming language do you hate the most?

      Results (450 votes). Check out past polls.