You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/Language/exceptions.pod6
+43-4Lines changed: 43 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -142,6 +142,21 @@ Any exception that is thrown in such a block
142
142
will be caught by the implicit C<CATCH> block or a C<CATCH> block provided by
143
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.
144
144
145
+
=begincode
146
+
try {
147
+
die "Tough luck";
148
+
say "Not gonna happen";
149
+
}
150
+
151
+
try {
152
+
fail "FUBAR";
153
+
}
154
+
=endcode
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
+
145
160
class E is Exception { method message() { "Just stop already!" } }
146
161
147
162
try {
@@ -176,10 +191,12 @@ Which would output:
176
191
in block <unit> at exception.p6 line 21
177
192
=endcode
178
193
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
+
179
198
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
@@ -203,7 +220,29 @@ say try "some-filename.txt".IO.slurp // "sane default";
203
220
# OUTPUT: «sane default»
204
221
=endcode
205
222
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
+
=begincode
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»
0 commit comments