4
4
5
5
= SUBTITLE Using exceptions in Perl 6
6
6
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.
11
10
12
11
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
14
13
backtrace printer.
15
14
16
15
= head1 Ad hoc exceptions
17
16
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.
20
19
21
20
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
23
23
24
24
= head1 Typed exceptions
25
25
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:
31
32
32
33
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
35
37
36
38
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 .
39
41
40
42
= head1 Catching exceptions
41
43
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:
43
45
44
46
die X::IO::DoesNotExist.new(:path("foo/bar"), :trying("zombie copy"));
47
+
45
48
CATCH {
46
49
when X::IO { say "some kind of IO exception was caught!" }
47
50
}
48
- #!> some kind of IO exception was caught!
51
+
52
+ # !> some kind of IO exception was caught!
49
53
50
54
Here, we are saying that if any exception of type C < X::IO > occurs, then the
51
55
message C < some kind of IO exception was caught! > will be displayed.
52
56
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.
56
60
57
- To handle all exceptions use a C < default > statement.
61
+ To handle all exceptions, use a C < default > statement.
58
62
59
63
CATCH {
60
64
default {
61
65
say .WHAT.perl, do given .backtrace[0] { .file, .line, .subname }
62
66
}
63
67
}
64
68
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
66
70
to match in the same manner, they must implement the given role. Just existing
67
71
in the same namespace will look alike but won't match in a C < CATCH > block.
68
72
69
73
= head2 Exception handlers and enclosing blocks.
70
74
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.
75
76
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.
76
78
77
79
die "something went wrong ...";
78
80
@@ -81,19 +83,14 @@ as the enclosing block gets left immediately:
81
83
default { .Str.say; }
82
84
}
83
85
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
91
88
92
89
Output:
93
90
94
91
something went wrong ...
95
92
96
- Compare with this one :
93
+ Compare with this:
97
94
98
95
CATCH {
99
96
@@ -111,13 +108,13 @@ Output:
111
108
112
109
Hi! I am at the outer block!
113
110
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.
115
112
116
113
= head1 X < C < try > |try blocks> blocks
117
114
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
119
116
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
121
118
rethrown.
122
119
123
120
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.
157
154
158
155
say try { +"99999" } // "oh no"
159
156
say try { +"hello" } // "oh no"
157
+
160
158
# OUTPUT « 99999oh no »
161
159
162
160
= head1 Throwing exceptions
163
161
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
165
163
C < Exception > object.
166
164
167
165
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.
174
172
when X::AdHoc { .resume }
175
173
}
176
174
}
175
+
177
176
"OBAI".say;
178
- #-> OHAI
179
- #-> OBAI
177
+
178
+ # -> OHAI
179
+ # -> OBAI
180
180
181
181
If the C < CATCH > block doesn't match the exception thrown, then the
182
182
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.
186
186
"OHAI".say;
187
187
CATCH { }
188
188
}
189
+
189
190
"OBAI".say;
190
- #!> foo
191
- #!> in block <unit> at my-script.p6:1
192
191
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
195
197
control continues after the C < CATCH > block.
196
198
197
199
{
@@ -201,8 +203,10 @@ control continues after the C<CATCH> block.
201
203
when X::AdHoc { }
202
204
}
203
205
}
206
+
204
207
"OBAI".say;
205
- #-> OBAI
208
+
209
+ # -> OBAI
206
210
207
211
C < throw > can be viewed as the method form of C < die > , just that in this
208
212
particular case, the sub and method forms of the routine have different
@@ -213,7 +217,7 @@ names.
213
217
Exceptions interrupt control flow and divert it away from the statement
214
218
following the statement that threw it. Any exception handled by the
215
219
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
217
221
method C < .resume > on the exception object.
218
222
219
223
CATCH { when X::AdHoc { .resume } } # this is step 2
@@ -236,7 +240,8 @@ printing a backtrace along with the message:
236
240
}
237
241
}
238
242
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
240
245
241
246
= head1 Control Exceptions
242
247
@@ -246,6 +251,7 @@ L<phaser|/language/phasers#Loop_Phasers>. Any unhandled control exception is
246
251
converted to a normal exception.
247
252
248
253
{ return; CATCH { default { say .^name, ': ',.Str } } }
254
+
249
255
# OUTPUT « X::ControlFlow::Return: Attempt to return outside of any Routine »
250
256
# was CX::Return
251
257
0 commit comments