develooper Front page | perl.perl5.porters | Postings from July 2013

Re: Try/Catch Exception Objects: Possible?

Thread Previous | Thread Next
From:
David E. Wheeler
Date:
July 26, 2013 12:08
Subject:
Re: Try/Catch Exception Objects: Possible?
Message ID:
24326482-9490-4CCB-9A32-AB462CC7818D@justatheory.com
On Jul 26, 2013, at 1:56 AM, David Nicol <davidnicol@gmail.com> wrote:

>>    package Foo {
>>        CATCH {
>>            # BOOM.
>>        }
>>    }
> 
> that looks like it should get optimized out, with a warning: AFAIK
> Perl has no syntax construct at this time that would change the scope
> of something depending on if something else is there or not.
> 
>     package Foo {
>         CATCH {
>             ... recovering from Foo:: error ...
>         }
>     }
>     package Foo {
>         sub riskything{
>              ...
>         };
>         CATCH {
>             ... recovering from error in Foo::riskything()
>         }
>     }

Yeah, that’s what I meant. Should also allow:

    package Foo {
        use Bar;
        my $f = Bar->baz;
        CATCH {
            # Recover from error anywhere in Foo
        }
    }

> Another question -- where does a package-scoped Catch return its value
> to? -- I imagine the answer would be, at the call stack boundary
> between the package that caught the error and a different package that
> called it.

Who says it has to return a value? It is a block, not a subroutine.

> That would support conventions such as returning -1 for errors
> implemented like so:
> 
>    FancyNewNetworkLibrary::CATCH { warn "unhandled exception ".shift @@; -1 }

Hrm. I see what you mean. If, in your above example, CATCH handles an error in riskything(), what gets returned from riskything()?

Best,

David
Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About