Skip to content

Commit cba03d5

Browse files
committed
Adds more examples of how try works
Which contributes to #1274.
1 parent 50d19c4 commit cba03d5

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

doc/Language/exceptions.pod6

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@ a description of the error:
2424
# RESULT: «oops, something went wrong in block <unit> at my-script.p6:1␤»
2525
2626
27-
It is worth noting that C<die> prints the error message to the standard error C<$*ERR>.
27+
It is worth noting that C<die> prints the error message to the standard error
28+
C<$*ERR>.
2829
2930
=head1 Typed exceptions
3031
3132
Typed exceptions provide more information about the error stored
3233
within an exception object.
3334
34-
For example, if while
35-
executing C<.zombie copy> on an object, a needed path C<foo/bar> becomes unavailable,
36-
then an L<X::IO::DoesNotExist> exception can be raised:
35+
For example, if while executing C<.zombie copy> on an object, a needed path
36+
C<foo/bar> becomes unavailable, then an L<X::IO::DoesNotExist> exception can be
37+
raised:
3738
3839
die X::IO::DoesNotExist.new(:path("foo/bar"), :trying("zombie copy"))
3940
@@ -121,10 +122,25 @@ See "Resuming of Exceptions", for how to return control back to where the except
121122
122123
=head1 X<C<try>|try>
123124
124-
To contain an exception, use a C<try> block. Any exception that is thrown in
125-
such a block will be caught by the implicit C<CATCH> block or a C<CATCH> block
126-
provided by the user. In the latter case, any unhandled exception will be
127-
rethrown.
125+
A C<try> block is a normal block with the
126+
L<C<use fatal> pragma|/language/pragmas#index-entry-fatal-fatal>
127+
turned on and an implicit C<CATCH> block that drops the exception, which means you can use it to contain them.
128+
129+
=begin code
130+
{
131+
my $x = +"a";
132+
say $x.^name;
133+
} # OUTPUT: «Failure␤»
134+
135+
try {
136+
my $x = +"a";
137+
say $x.^name;
138+
}
139+
=end code
140+
141+
Any exception that is thrown in such a block
142+
will be caught by the implicit C<CATCH> block or a C<CATCH> block provided by
143+
the user. In the latter case, any unhandled exception will be rethrown. If you choose not to handle the exception, they will be contained by the block.
128144
129145
class E is Exception { method message() { "Just stop already!" } }
130146
@@ -150,7 +166,7 @@ rethrown.
150166
say "No, you don't!";
151167
}
152168
153-
Output:
169+
Which would output:
154170
155171
=begin code :lang<text>
156172
I'm alive!
@@ -161,9 +177,9 @@ Output:
161177
=end code
162178
163179
A C<try>-block is a normal block and as such treats its last statement as the
164-
return value of itself. As a block, it will have by default turned on the
165-
L<C<use fatal> pragma|/language/pragmas#index-entry-fatal-fatal>. We can
166-
therefore use it as a right-hand side.
180+
return value of itself. We can therefore use it as a right-hand side. As a
181+
block, it will have by default turned on the
182+
L<C<use fatal> pragma|/language/pragmas#index-entry-fatal-fatal>.
167183
168184
=begin code
169185
say try { +"99999" } // "oh no"; # OUTPUT: «99999␤»
@@ -187,6 +203,8 @@ say try "some-filename.txt".IO.slurp // "sane default";
187203
# OUTPUT: «sane default␤»
188204
=end code
189205
206+
What C<try> actually causes is, via the C<use fatal> pragma, a immediate throw of the exceptions that happen within its scope, but by doing so the C<CATCH> block is invoked from the point where the exception is thrown, which defines its scope.
207+
190208
=head1 Throwing exceptions
191209
192210
Exceptions can be thrown explicitly with the C<.throw> method of an

0 commit comments

Comments
 (0)