Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: A couple questions about object oriented Perl programming: inheritance and error handling

by stephen (Priest)
on Apr 17, 2013 at 23:30 UTC ( #1029236=note: print w/ replies, xml ) Need Help??


in reply to A couple questions about object oriented Perl programming: inheritance and error handling

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.

stephen


Comment on Re: A couple questions about object oriented Perl programming: inheritance and error handling
Download Code
Re^2: A couple questions about object oriented Perl programming: inheritance and error handling (eval $@ truth)
by Anonymous Monk on Apr 18, 2013 at 04:13 UTC

    $@ might not be a true value :) or other things, see Try::Tiny and Devel::EvalError for a description of the possible issue

    eval { execeptionalWithoutReturnValue(); 1; } or do { my $exception = $@; ... };

    Or

    use Try::Tiny;
    try {
      execeptionalWithoutReturnValue();
    } catch {
      warn "caught error: $_"; # not $@
    };
    

    Or

    use Devel::EvalError();
    my $ee = Deval::EvalError->new();
    $ee->ExpectOne(
        eval {
            execeptionalWithoutReturnValue();
            1;
        }
    );
    if( $ee->Failed() ) {    # if ( ! $ee->Succeeded() )
        ... $ee->Reason() ...;
    }
    

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1029236]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2014-07-31 10:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (248 votes), past polls