Exception Handling, DivideByZero

I was just looking into doing some finer-grained exception handling,
so I tried this:

   use v6;
   try {
       my $result = 4/0;
       say "result: $result";
       CATCH {
   #        when DivideByZero { say "Oh, you know."; }
           default { say .WHAT; .Str.say } # (DivideByZero)   Attempt
to divide 4 by zero using div
       }
   }

The first time through, The .WHAT tells me I've got
"DivideByZero", and so I added the line that's commented out
here, at which point I got the error:

   ===SORRY!===
   Function 'DivideByZero' needs parens to avoid gobbling block (or
perhaps it's a class that's not declared or available in this scope?)

Putting parens around (DivideByZero) doesn't help:

   Undeclared name:
      DivideByZero used at line 12

My impression was this would just work from looking
at the examples using things like X::AdHoc here:

  https://docs.perl6.org/language/exceptions
0
doomvox
10/29/2018 5:03:11 PM
perl.perl6.users 1103 articles. 0 followers. Follow

3 Replies
21 Views

Similar Articles

[PageSpeed] 2

--0000000000009e6b830579612338
Content-Type: text/plain; charset="UTF-8"

Two issues:

(1) all standard exceptions are in or under the X:: namespace.

(2) .WHAT doesn't show names with their namespaces, whereas .^name does.

pyanfar Z$ 6 'my $r = 4/0; say $r; CATCH {default {say .^name}}'
X::Numeric::DivideByZero


On Mon, Oct 29, 2018 at 1:04 PM Joseph Brenner <doomvox@gmail.com> wrote:

> I was just looking into doing some finer-grained exception handling,
> so I tried this:
>
>    use v6;
>    try {
>        my $result = 4/0;
>        say "result: $result";
>        CATCH {
>    #        when DivideByZero { say "Oh, you know."; }
>            default { say .WHAT; .Str.say } # (DivideByZero)   Attempt
> to divide 4 by zero using div
>        }
>    }
>
> The first time through, The .WHAT tells me I've got
> "DivideByZero", and so I added the line that's commented out
> here, at which point I got the error:
>
>    ===SORRY!===
>    Function 'DivideByZero' needs parens to avoid gobbling block (or
> perhaps it's a class that's not declared or available in this scope?)
>
> Putting parens around (DivideByZero) doesn't help:
>
>    Undeclared name:
>       DivideByZero used at line 12
>
> My impression was this would just work from looking
> at the examples using things like X::AdHoc here:
>
>   https://docs.perl6.org/language/exceptions
>


-- 
brandon s allbery kf8nh
allbery.b@gmail.com

--0000000000009e6b830579612338
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr">Two issues:<div><br></div><div>(1) all st=
andard exceptions are in or under the X:: namespace.</div><div><br></div><d=
iv>(2) .WHAT doesn&#39;t show names with their namespaces, whereas .^name d=
oes.</div><div><br></div><div><div>pyanfar Z$ 6 &#39;my $r =3D 4/0; say $r;=
 CATCH {default {say .^name}}&#39;</div><div>X::Numeric::DivideByZero</div>=
</div><div><br></div></div></div><br><div class=3D"gmail_quote"><div dir=3D=
"ltr">On Mon, Oct 29, 2018 at 1:04 PM Joseph Brenner &lt;<a href=3D"mailto:=
doomvox@gmail.com">doomvox@gmail.com</a>&gt; wrote:<br></div><blockquote cl=
ass=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;p=
adding-left:1ex">I was just looking into doing some finer-grained exception=
 handling,<br>
so I tried this:<br>
<br>
=C2=A0 =C2=A0use v6;<br>
=C2=A0 =C2=A0try {<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0my $result =3D 4/0;<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0say &quot;result: $result&quot;;<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0CATCH {<br>
=C2=A0 =C2=A0#=C2=A0 =C2=A0 =C2=A0 =C2=A0 when DivideByZero { say &quot;Oh,=
 you know.&quot;; }<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0default { say .WHAT; .Str.say } # =
(DivideByZero)=C2=A0 =C2=A0Attempt<br>
to divide 4 by zero using div<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0}<br>
=C2=A0 =C2=A0}<br>
<br>
The first time through, The .WHAT tells me I&#39;ve got<br>
&quot;DivideByZero&quot;, and so I added the line that&#39;s commented out<=
br>
here, at which point I got the error:<br>
<br>
=C2=A0 =C2=A0=3D=3D=3DSORRY!=3D=3D=3D<br>
=C2=A0 =C2=A0Function &#39;DivideByZero&#39; needs parens to avoid gobbling=
 block (or<br>
perhaps it&#39;s a class that&#39;s not declared or available in this scope=
?)<br>
<br>
Putting parens around (DivideByZero) doesn&#39;t help:<br>
<br>
=C2=A0 =C2=A0Undeclared name:<br>
=C2=A0 =C2=A0 =C2=A0 DivideByZero used at line 12<br>
<br>
My impression was this would just work from looking<br>
at the examples using things like X::AdHoc here:<br>
<br>
=C2=A0 <a href=3D"https://docs.perl6.org/language/exceptions" rel=3D"norefe=
rrer" target=3D"_blank">https://docs.perl6.org/language/exceptions</a><br>
</blockquote></div><br clear=3D"all"><div><br></div>-- <br><div dir=3D"ltr"=
 class=3D"gmail_signature" data-smartmail=3D"gmail_signature"><div dir=3D"l=
tr"><div><div dir=3D"ltr"><div>brandon s allbery kf8nh</div><div><a href=3D=
"mailto:allbery.b@gmail.com" target=3D"_blank">allbery.b@gmail.com</a></div=
></div></div></div></div>

--0000000000009e6b830579612338--
0
allbery
10/29/2018 5:09:48 PM
Brandon Allbery <allbery.b@gmail.com> wrote:
> Two issues:
>
> (1) all standard exceptions are in or under the X:: namespace.
>
> (2) .WHAT doesn't show names with their namespaces, whereas .^name does.
>
> pyanfar Z$ 6 'my $r = 4/0; say $r; CATCH {default {say .^name}}'
> X::Numeric::DivideByZero

Thanks.  I didn't get that the behavior of WHAT and ^name were that different.



> On Mon, Oct 29, 2018 at 1:04 PM Joseph Brenner <doomvox@gmail.com> wrote:
>
>> I was just looking into doing some finer-grained exception handling,
>> so I tried this:
>>
>>    use v6;
>>    try {
>>        my $result = 4/0;
>>        say "result: $result";
>>        CATCH {
>>    #        when DivideByZero { say "Oh, you know."; }
>>            default { say .WHAT; .Str.say } # (DivideByZero)   Attempt
>> to divide 4 by zero using div
>>        }
>>    }
>>
>> The first time through, The .WHAT tells me I've got
>> "DivideByZero", and so I added the line that's commented out
>> here, at which point I got the error:
>>
>>    ===SORRY!===
>>    Function 'DivideByZero' needs parens to avoid gobbling block (or
>> perhaps it's a class that's not declared or available in this scope?)
>>
>> Putting parens around (DivideByZero) doesn't help:
>>
>>    Undeclared name:
>>       DivideByZero used at line 12
>>
>> My impression was this would just work from looking
>> at the examples using things like X::AdHoc here:
>>
>>   https://docs.perl6.org/language/exceptions
>>
>
>
> --
> brandon s allbery kf8nh
> allbery.b@gmail.com
>
0
doomvox
10/29/2018 5:12:09 PM
..WHAT gives you the actual type, whereas .^name gives you a string
that is the name.

In Perl6 types are things you can pass around; which is good because
you can have more than one with the same name.

    sub bar (){
      my class Foo {  }
    }
    sub baz (){
      my class Foo {  }
    }

    my $a = bar().new();

    bar() === $a.^name; # False (one is a Foo type object, the other a string)
    bar() === $a.WHAT; # True (both are the Foo type object)

    bar().^name === baz().^name; # True (but they are different types)
    bar().WHAT === baz().WHAT; # False (both are type objects, but
they are different types)

    "".WHAT =:= Str; # True (both are the Str type object)

Basically think about .WHAT as being the Perl6 equivalent of `typeof()`.
On Mon, Oct 29, 2018 at 12:12 PM Joseph Brenner <doomvox@gmail.com> wrote:
>
> Brandon Allbery <allbery.b@gmail.com> wrote:
> > Two issues:
> >
> > (1) all standard exceptions are in or under the X:: namespace.
> >
> > (2) .WHAT doesn't show names with their namespaces, whereas .^name does.
> >
> > pyanfar Z$ 6 'my $r = 4/0; say $r; CATCH {default {say .^name}}'
> > X::Numeric::DivideByZero
>
> Thanks.  I didn't get that the behavior of WHAT and ^name were that different.
>
>
>
> > On Mon, Oct 29, 2018 at 1:04 PM Joseph Brenner <doomvox@gmail.com> wrote:
> >
> >> I was just looking into doing some finer-grained exception handling,
> >> so I tried this:
> >>
> >>    use v6;
> >>    try {
> >>        my $result = 4/0;
> >>        say "result: $result";
> >>        CATCH {
> >>    #        when DivideByZero { say "Oh, you know."; }
> >>            default { say .WHAT; .Str.say } # (DivideByZero)   Attempt
> >> to divide 4 by zero using div
> >>        }
> >>    }
> >>
> >> The first time through, The .WHAT tells me I've got
> >> "DivideByZero", and so I added the line that's commented out
> >> here, at which point I got the error:
> >>
> >>    ===SORRY!===
> >>    Function 'DivideByZero' needs parens to avoid gobbling block (or
> >> perhaps it's a class that's not declared or available in this scope?)
> >>
> >> Putting parens around (DivideByZero) doesn't help:
> >>
> >>    Undeclared name:
> >>       DivideByZero used at line 12
> >>
> >> My impression was this would just work from looking
> >> at the examples using things like X::AdHoc here:
> >>
> >>   https://docs.perl6.org/language/exceptions
> >>
> >
> >
> > --
> > brandon s allbery kf8nh
> > allbery.b@gmail.com
> >
0
b2gills
10/29/2018 11:51:30 PM
Reply: