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
turned on and an implicit C<CATCH> block that drops the exception, which means you can use it to contain them.
128
+
129
+
=begincode
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
+
=endcode
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.
128
144
129
145
class E is Exception { method message() { "Just stop already!" } }
130
146
@@ -150,7 +166,7 @@ rethrown.
150
166
say "No, you don't!";
151
167
}
152
168
153
-
Output:
169
+
Which would output:
154
170
155
171
=begincode :lang<text>
156
172
I'm alive!
@@ -161,9 +177,9 @@ Output:
161
177
=endcode
162
178
163
179
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
@@ -187,6 +203,8 @@ say try "some-filename.txt".IO.slurp // "sane default";
187
203
# OUTPUT: «sane default»
188
204
=endcode
189
205
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
+
190
208
=head1Throwing exceptions
191
209
192
210
Exceptions can be thrown explicitly with the C<.throw> method of an
0 commit comments