Skip to content

Commit af89aa5

Browse files
authored
Merge pull request #1172 from antquinonez/exceptions-cleanup
edits to exceptions pod
2 parents d1788ca + 1a9471c commit af89aa5

File tree

1 file changed

+55
-49
lines changed

1 file changed

+55
-49
lines changed

doc/Language/exceptions.pod6

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,75 +4,77 @@
44
55
=SUBTITLE Using exceptions in Perl 6
66
7-
Exceptions in Perl 6 are a special kind of object used to signify when
8-
something has gone wrong, for instance, unexpected data was received, a
9-
network connection is no longer available, or a file is missing which was
10-
expected to exist.
7+
Exceptions in Perl 6 are objects that record error information; for example:
8+
that unexpected data was received; that a
9+
network connection is no longer available; or that a needed file is missing.
1110
1211
All built-in exceptions inherit from L<Exception>, which provides some basic
13-
behavior, such as storing a backtrace and providing an interface for the
12+
behavior, including the storage of a backtrace and an interface for the
1413
backtrace printer.
1514
1615
=head1 Ad hoc exceptions
1716
18-
Ad hoc exceptions work just like in traditional Perl 5, one can simply use
19-
C<die> with a message as to what went wrong:
17+
Ad hoc exceptions work just like in Perl 5, where C<die> is called with
18+
a description of the error.
2019
2120
die "oops, something went wrong";
22-
#!> oops, something went wrong in block <unit> at my-script.p6:1
21+
22+
# !> oops, something went wrong in block <unit> at my-script.p6:1
2323
2424
=head1 Typed exceptions
2525
26-
Typed exceptions provide more information about the kind of error that
27-
occurred within the exception object itself. For instance, if while
28-
executing C<.zombie copy> on an object the path C<foo/bar> is unavailable
29-
(and was expected to be available), then one could raise an
30-
L<X::IO::DoesNotExist> exception like so:
26+
Typed exceptions provide more information about the error stored
27+
within an exception object.
28+
29+
For example, if while
30+
executing C<.zombie copy> on an object, a needed path C<foo/bar> becomes unavailable,
31+
then an L<X::IO::DoesNotExist> exception can be raised:
3132
3233
die X::IO::DoesNotExist.new(:path("foo/bar"), :trying("zombie copy"))
33-
#!> Failed to find 'foo/bar' while trying to do '.zombie copy'
34-
#!> in block <unit> at my-script.p6:1
34+
35+
# !> Failed to find 'foo/bar' while trying to do '.zombie copy'
36+
# !> in block <unit> at my-script.p6:1
3537
3638
Note how the object has provided the backtrace with information about what
37-
went wrong so that hopefully the user of the code can find and correct the
38-
issue more easily.
39+
went wrong. A user of the code can now more easily find and correct the
40+
problem.
3941
4042
=head1 Catching exceptions
4143
42-
It is possible to handle exceptional circumstances by supplying a C<CATCH> block:
44+
It's possible to handle exceptional circumstances by supplying a C<CATCH> block:
4345
4446
die X::IO::DoesNotExist.new(:path("foo/bar"), :trying("zombie copy"));
47+
4548
CATCH {
4649
when X::IO { say "some kind of IO exception was caught!" }
4750
}
48-
#!> some kind of IO exception was caught!
51+
52+
# !> some kind of IO exception was caught!
4953
5054
Here, we are saying that if any exception of type C<X::IO> occurs, then the
5155
message C<some kind of IO exception was caught!> will be displayed.
5256
53-
A X<C<CATCH>|CATCH> block uses smart matching similarly to how C<given/when> smart
54-
matches on options, thus it is possible to catch various categories of
55-
exceptions and handle them appropriately inside a C<when> block.
57+
A X<C<CATCH>|CATCH> block uses smart matching similar to how C<given/when> smart
58+
matches on options, thus it's possible to catch and handle various categories of
59+
exceptions inside a C<when> block.
5660
57-
To handle all exceptions use a C<default> statement.
61+
To handle all exceptions, use a C<default> statement.
5862
5963
CATCH {
6064
default {
6165
say .WHAT.perl, do given .backtrace[0] { .file, .line, .subname }
6266
}
6367
}
6468
65-
Please note that the match target is a role. To allow user defined exceptions
69+
Note that the match target is a role. To allow user defined exceptions
6670
to match in the same manner, they must implement the given role. Just existing
6771
in the same namespace will look alike but won't match in a C<CATCH> block.
6872
6973
=head2 Exception handlers and enclosing blocks.
7074
71-
After a CATCH has handled the exception, the block enclosing the CATCH is left.
72-
73-
In other words, even when the exception is handled successfully, the I<rest of the code> in the enclosing block will never be executed
74-
as the enclosing block gets left immediately:
75+
After a CATCH has handled the exception, the block enclosing the CATCH is exited.
7576
77+
In other words, even when the exception is handled successfully, the I<rest of the code> in the enclosing block will never be executed.
7678
7779
die "something went wrong ...";
7880
@@ -81,19 +83,14 @@ as the enclosing block gets left immediately:
8183
default { .Str.say; }
8284
}
8385
84-
# but this line will be never reached
85-
# as once default exception handler
86-
# gets executed
87-
# a enclosing block - mainline of the program
88-
# will be left immediately
89-
90-
say "This won't be said.";
86+
say "This won't be said."; # but this line will be never reached since
87+
# the enclosing block will be exited immediately
9188
9289
Output:
9390
9491
something went wrong ...
9592
96-
Compare with this one:
93+
Compare with this:
9794
9895
CATCH {
9996
@@ -111,13 +108,13 @@ Output:
111108
112109
Hi! I am at the outer block!
113110
114-
See also "Resuming of Exceptions" to return control back to where the exception originated.
111+
See "Resuming of Exceptions", for how to return control back to where the exception originated.
115112
116113
=head1 X<C<try>|try blocks> blocks
117114
118-
To contain an exception use a C<try> block. Any exception that is thrown in
115+
To contain an exception, use a C<try> block. Any exception that is thrown in
119116
such a block will be caught by the implicit C<CATCH> block or a C<CATCH> block
120-
provided by the user. In the latter case, any exception not handled will be
117+
provided by the user. In the latter case, any unhandled exception will be
121118
rethrown.
122119
123120
class E is Exception { method message() { "Just stop already!" } }
@@ -157,11 +154,12 @@ return value of itself. We can therefore use it as a RHS.
157154
158155
say try { +"99999" } // "oh no"
159156
say try { +"hello" } // "oh no"
157+
160158
# OUTPUT«99999␤oh no␤»
161159
162160
=head1 Throwing exceptions
163161
164-
One can also explicitly throw exceptions via the C<.throw> method on an
162+
Exceptions can be thrown explicitly with the C<.throw> method of an
165163
C<Exception> object.
166164
167165
This example throws an C<AdHoc> exception, catches it and allows the code
@@ -174,9 +172,11 @@ to continue from the point of the exception by calling the C<.resume> method.
174172
when X::AdHoc { .resume }
175173
}
176174
}
175+
177176
"OBAI".say;
178-
#-> OHAI
179-
#-> OBAI
177+
178+
# -> OHAI
179+
# -> OBAI
180180
181181
If the C<CATCH> block doesn't match the exception thrown, then the
182182
exception's payload is passed on to the backtrace printing mechanism.
@@ -186,12 +186,14 @@ exception's payload is passed on to the backtrace printing mechanism.
186186
"OHAI".say;
187187
CATCH { }
188188
}
189+
189190
"OBAI".say;
190-
#!> foo
191-
#!> in block <unit> at my-script.p6:1
192191
193-
This example doesn't resume from the point of the exception, however
194-
it continues after the enclosing block, since the exception was caught, and
192+
# !> foo
193+
# !> in block <unit> at my-script.p6:1
194+
195+
This next example doesn't resume from the point of the exception. Instead,
196+
it continues after the enclosing block, since the exception is caught, and then
195197
control continues after the C<CATCH> block.
196198
197199
{
@@ -201,8 +203,10 @@ control continues after the C<CATCH> block.
201203
when X::AdHoc { }
202204
}
203205
}
206+
204207
"OBAI".say;
205-
#-> OBAI
208+
209+
# -> OBAI
206210
207211
C<throw> can be viewed as the method form of C<die>, just that in this
208212
particular case, the sub and method forms of the routine have different
@@ -213,7 +217,7 @@ names.
213217
Exceptions interrupt control flow and divert it away from the statement
214218
following the statement that threw it. Any exception handled by the
215219
user can be resumed and control flow will continue with the statement
216-
following the statement that threw the exception. To do so call the
220+
following the statement that threw the exception. To do so, call the
217221
method C<.resume> on the exception object.
218222
219223
CATCH { when X::AdHoc { .resume } } # this is step 2
@@ -236,7 +240,8 @@ printing a backtrace along with the message:
236240
}
237241
}
238242
die X::WithoutLineNumber.new(payload => "message")
239-
# prints "message\n" to $*ERR and exits, no backtrace
243+
244+
# prints "message\n" to $*ERR and exits, no backtrace
240245
241246
=head1 Control Exceptions
242247
@@ -246,6 +251,7 @@ L<phaser|/language/phasers#Loop_Phasers>. Any unhandled control exception is
246251
converted to a normal exception.
247252
248253
{ return; CATCH { default { say .^name, ': ',.Str } } }
254+
249255
# OUTPUT«X::ControlFlow::Return: Attempt to return outside of any Routine␤»
250256
# was CX::Return
251257

0 commit comments

Comments
 (0)