@@ -94,43 +94,30 @@ backtrace.
94
94
95
95
Defined as:
96
96
97
- multi sub fail(*@text)
98
97
multi sub fail(Exception $e)
99
- method fail(Exception:D:)
98
+ method fail(Exception:D:)
100
99
101
100
Exits the calling C < Routine > and returns a L < Failure|/type/Failure > object wrapping the
102
- exception C < $e > - or, for the C < *@text > form, an L < X::AdHoc|/type/X::AdHoc > exception
103
- constructed from the concatenation of C < @text > . If the caller
104
- activated fatal exceptions via the pragma C < use fatal; > , the exception is
105
- thrown instead of being returned as a C < Failure > .
101
+ exception.
106
102
103
+ = begin code
104
+ # A custom exception defined
105
+ class ForbiddenWord is Exception {
106
+ has Str $.word;
107
+ method message { "This word is forbidden: «$!word»" }
108
+ }
109
+
110
+ sub say-word ( $word ) {
111
+ ForbiddenWord.new(:word($word)).fail if $word eq 'foo';
112
+ $word.say;
113
+ }
114
+
115
+ my $result = say-word("foo");
116
+ say $result.exception;
117
+ = end code
107
118
108
- # A custom exception defined
109
- class ForbiddenDirectory is Exception {
110
- has Str $.name;
111
-
112
- method message { "This directory is forbidden: '$!name'" }
113
- }
114
-
115
- sub copy-directory-tree ($dir) {
116
- # We don't allow for non-directories to be copied
117
- fail "$dir is not a directory" if !$dir.IO.d;
118
- # We don't allow 'foo' directory to be copied too
119
- fail ForbiddenDirectory.new(:name($dir)) if $dir eq 'foo';
120
- # or above can be written in method form as:
121
- # ForbiddenDirectory.new(:name($dir)).fail if $dir eq 'foo';
122
- # Do some actual copying here
123
- ...
124
- }
125
-
126
- # A Failure with X::AdHoc exception object is returned and
127
- # assigned, so no throwing Would be thrown without an assignment
128
- my $result = copy-directory-tree("cat.jpg");
129
- say $result.exception; # OUTPUT: «cat.jpg is not a directory»
130
-
131
- # A Failure with a custom Exception object is returned
132
- $result = copy-directory-tree('foo');
133
- say $result.exception; # OUTPUT: «This directory is forbidden: 'foo'»
119
+ The routine form works in the same way, with an alternative syntax:
120
+ C < fail ForbiddenWord.new(:word($word)) > .
134
121
135
122
= head2 method gist
136
123
0 commit comments