Glenn Linderman wrote:
> 
> Here's some code that returns one non-fatal error.  I'd like to
> change it to use the new RFC 88 mechanism.  Please show me how.
> I've included how to do it via RFC 119.  Note that sub
> really_delicate_code is documented that only one possible
> non-fatal error can occur, although a variety of fatal errors
> are possible, hence by the time ... error handling code ... is
> invoked, it can assume that is the error that occurred.
> 
> [input code snipped]
> 
> with RFC 199, the conversion is simple:
> 
> sub really_delicate_code {
>   my ( $o, $my ) = @_;
>   die "$o > 100"  if $o > 100;
>   die "insufficient flibbertygibbets"  if $o < 43;
>   die "you're out of your mind"  if $my > $mind; # $mind is global, I
> guess
>   throw "$my > 100" if $my > 100; # here's the non-fatal error
>   return $o + $my;
> }
> 
> $foo = 300;
> $bar = 300;
> { $result = & really_delicate_code ( $foo, $bar );
>    catch { ... error handling code here ... };
> }

How would I write it?  Hmm, hmm, hmm.  Rustle, rustle.  Oops, no,
damn.  Hmm.  Rustle.  Hmm, hmm, hmm.  Ok.  I'd do it like this:

    sub really_delicate_code
    {
        my ($o, $my) = @_;

        $o > 100 and throw Exception "$o > 100", severity => "fatal";

        $o < 43 and throw Exception "Insufficient", severity => "fatal";

        $my > $mind and throw Exception "Crazy", severity => "fatal";

        $my > 100 and throw Exception "$my > 100";

        return $o + $my;
        }

    $foo = 300; $bar = 300;

    try { $result = &really_delicate_code($foo, $bar); }

    catch !$@->fatal => {

        ... error handling code here ...
        }

But really, your original example is broken.  What if some "throw
Exception" raises a low-level cant-open-file error because, say,
it is trying to log exceptions?  That's a non-fatal error, so you
still invoke your error handling code, but now you can't be so
sure it's because $my > 100.  So, how I would really write it, if
I weren't transliterating the mistakes and were using RFC 88?

    exception 'My100';

    sub really_delicate_code
    {
        my ($o, $my) = @_;

        $o > 100 and throw Exception "$o > 100";

        $o < 43 and throw Exception "Insufficient";

        $my > $mind and throw Exception "Crazy";

        $my > 100 and throw My100 "$my > 100";

        return $o + $my;
        }

    $foo = 300; $bar = 300;

    try { $result = &really_delicate_code($foo, $bar); }

    catch My100 => { ... error handling code here ... };

Well, not in production code, of course.  There all the Exception
classes would be properly subclassed, the message ivars would be
useful, a unique tag would be specified, debug info would be
provided, &c.  But you get the idea.

And as you can see, when correctly coded, the concept of fatality
doesn't enter into it.

Yours, &c, Tony Olekshy

Reply via email to