Skip to content

Commit d4f82dd

Browse files
committed
Adds indexing and some clarification of the resume statement, refs #1274
1 parent cba03d5 commit d4f82dd

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

doc/Language/exceptions.pod6

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,21 @@ Any exception that is thrown in such a block
142142
will be caught by the implicit C<CATCH> block or a C<CATCH> block provided by
143143
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.
144144
145+
=begin code
146+
try {
147+
die "Tough luck";
148+
say "Not gonna happen";
149+
}
150+
151+
try {
152+
fail "FUBAR";
153+
}
154+
=end code
155+
156+
X<|resume (Exceptions)>
157+
In both C<try> blocks above, exceptions will be contained within the block, but
158+
the C<say> statement will not be run. We can handle them, though:
159+
145160
class E is Exception { method message() { "Just stop already!" } }
146161
147162
try {
@@ -176,10 +191,12 @@ Which would output:
176191
in block <unit> at exception.p6 line 21
177192
=end code
178193
194+
Since the C<CATCH> block is handling just the C<X::AdHoc> exception thrown by
195+
the C<die> statement, but not the C<E> exception. In the absence of a C<CATCH>
196+
block, all exceptions will be contained and dropped, as indicated above. C<resume> will resume execution right after the exception has been thrown; in this case, in the C<die> statement.
197+
179198
A C<try>-block is a normal block and as such treats its last statement as the
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>.
199+
return value of itself. We can therefore use it as a right-hand side.
183200
184201
=begin code
185202
say try { +"99999" } // "oh no"; # OUTPUT: «99999␤»
@@ -203,7 +220,29 @@ say try "some-filename.txt".IO.slurp // "sane default";
203220
# OUTPUT: «sane default␤»
204221
=end code
205222
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.
223+
What C<try> actually causes is, via the C<use fatal> pragma, a immediate throw
224+
of the exceptions that happen within its scope, but by doing so the C<CATCH>
225+
block is invoked from the point where the exception is thrown, which defines its
226+
scope.
227+
228+
=begin code
229+
my $error-code = "333";
230+
sub bad-sub {
231+
die "Something bad happened";
232+
}
233+
try {
234+
my $error-code = "111";
235+
bad-sub;
236+
237+
CATCH {
238+
default {
239+
say "Error $error-code ", .^name, ': ',.Str
240+
}
241+
}
242+
}
243+
# OUTPUT: «Error 111 X::AdHoc: Something bad happened␤»
244+
=end code
245+
207246
208247
=head1 Throwing exceptions
209248

0 commit comments

Comments
 (0)