On 3/6/06, Tom Allison <[EMAIL PROTECTED]> wrote:
> Jay Savage wrote:
>
> >
> > die on errors and just keep passing them up the line:
> >
> >     eval {
> >         eval {
> >             eval {
> >                 bad_system_call() or die "$!\n";
> >             } or die $@;
> >         } or die $@;
> >     };
> >
> >     print "eval says: [EMAIL PROTECTED]" if $@;
> >
> > As long as you keep propagating $@ in your die calls, the original
> > error message will get passed out.
> >
>
> I'm getting mixed messages between what you present and the following (I noted
> the 'or' being a huge difference):
>
> eval {
>      eval { success() or die $! };
>      if ($@ =~ /a peaceful death/) {
>         euthenasia();
>         return 0;
>      } else {
>         die;  # no args!  Will this propogate?
>      }
> };
> if ( $@ =~ /something else/ ) {
>    print "something happened that's not fatal\n";
>    return 0;
> } else {
>    die [EMAIL PROTECTED];
> }
> return 1;
>
> There's a distinct difference (in my own mind at least) between the different
> errors or responses that an applicatoin/method can have.
>
> There's a fatal error that needs to just kill the application.
> There's a really bad error that needs to be mentioned but at least I can 
> respond
> to the client (net work connection)  (return 0)
> There's a minor error to report to the client  (return 0).
> There's a success process  (return 1).
>
> So I guess the questions that I'm playing with are:
> Is my small code block about right?
> Will 'die' by itself propogate the contents of $@ ad infinitum?
>
> Is it as hard as I think it is to test exception handling?
> I can test some of them, but I can't test the FATAL stuff (unless I code 
> munge).
>

die doesn't propigate the error, it returns a failure, along with
whatever you ask it to return. So if you say 'die $@', it passes along
the value of [EMAIL PROTECTED] Just as, when you use 'die $!' on a system call, 
it
passes on the error generated by the system. If you just say die,

Since eval traps die, 'eval{ eval {} or die [EMAIL PROTECTED];};' is a 
convenient
method for propagating $@ out of deeply nested evals. It's just a way
to save writing lots of if blocks that do nothing but pass $@ along.
The efect is the same as :

eval {
    eval {} ;
    if ($@) {
        die $@;
    }
};

The idea is to propagate $@ to some outer eval--or the main
routine--where you can deal with your error handling in a centralized
way, something like:

eval {

    eval {
        some;
        snippet;
        of;
        code, perhaps with an;
            eval {} or die $@;
    } or die $@;

    eval {
        another;
        block;
        of;
        code;
    } or die $@ ;
};

if ($@) { # there was an error in the nested evals
    log_error if $@ =~ /something/;
    write_error_to_socket if $@ =~/something else/;
    rollback_database if $@ =~ /something else again/;
    die "You're dead: [EMAIL PROTECTED]" if $@ =~ /is fatal/;
}

HTH,

-- jay
--------------------------------------------------
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.dpguru.com  http://www.engatiki.org

values of β will give rise to dom!

Reply via email to