Perl: the Markov chain saw | |
PerlMonks |
Re: A couple questions about object oriented Perl programming: inheritance and error handlingby stephen (Priest) |
on Apr 17, 2013 at 23:30 UTC ( [id://1029236]=note: 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:
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. stephen
In Section
Seekers of Perl Wisdom
|
|